List of usage examples for java.lang IndexOutOfBoundsException IndexOutOfBoundsException
public IndexOutOfBoundsException(int index)
From source file:Main.java
public static String getShortWeekdayName(int day) { if (day < 1 || day > 7) throw new IndexOutOfBoundsException("Invalid index, value must be between 1 and 7"); String dayName = dateFormatSymbols.getShortWeekdays()[day]; return dayName.replace(dayName.charAt(0), Character.toUpperCase(dayName.charAt(0))); }
From source file:Main.java
public static String getMonthNameFromLocale(int month) { if (month < 0 || month > 11) throw new IndexOutOfBoundsException("Invalid index, value must be between 0 and 11"); return dateFormatSymbols.getMonths()[month].toUpperCase(); }
From source file:Main.java
/** * Copies all of the elements from one list into another. After the * operation, the index of each copied element in the destination list will * be identical to its index in the source list. The destination list must * be at least as long as the source list. If it is longer, the remaining * elements in the destination list are unaffected. * <p>// ww w . jav a 2 s . com * This method runs in linear time. * @param <T> . * @param dest The destination list. * @param src The source list. * * @return boolean isCopyValide */ public static <T> Boolean copy(List<? super T> dest, List<? extends T> src) { Boolean isCopyValide = null; int srcSize = src.size(); if (srcSize > dest.size()) { isCopyValide = false; throw new IndexOutOfBoundsException("Source does not fit in dest"); } if (srcSize < COPY_THRESHOLD || (src instanceof RandomAccess && dest instanceof RandomAccess)) { for (int i = 0; i < srcSize; i++) { dest.set(i, src.get(i)); } } else { ListIterator<? super T> di = dest.listIterator(); ListIterator<? extends T> si = src.listIterator(); for (int i = 0; i < srcSize; i++) { di.next(); di.set(si.next()); } } isCopyValide = true; return isCopyValide; }
From source file:Main.java
/** * Ensures an index is not negative./*from w w w . j a v a2s.c om*/ * @param index the index to check. * @throws IndexOutOfBoundsException if the index is negative. */ static void checkIndexBounds(final int index) { if (index < 0) { throw new IndexOutOfBoundsException("Index cannot be negative: " + index); } }
From source file:Util.java
/********************************************************************* * Inserts an Object into an Object array at the index position. * * <p>/*from w ww . j ava 2s. c o m*/ * Example: * <code> * <pre> * String [ ] stringArray * = ( String [ ] ) ArrayLib.insert ( new String [ ] { }, "", 0 ); * </pre> * </code> * </p> * * @throws NullArgumentException * * If objectArray or o is null. * * @throws IndexOutOfBoundsException * * If index < 0 or index > objectArray.length. * * @return * * Returns a new array with the same component type as the old array. *********************************************************************/ public static Object[] insert(Object[] objectArray, Object o, int index) ////////////////////////////////////////////////////////////////////// { if ((index < 0) || (index > objectArray.length)) { throw new IndexOutOfBoundsException("index out of range: " + index); } Object[] newObjectArray = (Object[]) Array.newInstance(objectArray.getClass().getComponentType(), objectArray.length + 1); System.arraycopy(objectArray, 0, newObjectArray, 0, index); newObjectArray[index] = o; System.arraycopy(objectArray, index, newObjectArray, index + 1, objectArray.length - index); return newObjectArray; }
From source file:Main.java
/** * Implementation of the OCL// ww w . ja va2 s . c om * <ul> * <li><tt>OrderedSet::at(index : Integer) : T</tt></li> * <li><tt>Sequence::at(index : Integer) : T</tt></li> * </ul> * operations. * * @param self the source collection * @param index the 1-based (in OCL fashion) index * @return the object at the specified index of the source collection * * @throws IndexOutOfBoundsException if the index is out of bounds */ public static <E> E at(Collection<E> self, int index) { index = index - 1; if (index < 0 || index >= self.size()) { throw new IndexOutOfBoundsException("index: " + (index + 1) + ", size: " //$NON-NLS-1$ //$NON-NLS-2$ + self.size()); } int curr = 0; for (Iterator<E> it = self.iterator(); it.hasNext();) { E object = it.next(); if (curr++ == index) { return object; } } return null; // undefined }
From source file:ByteArrayList.java
private void rangeCheck(int i) { if (i < 0 || i >= m_size) throw new IndexOutOfBoundsException("Index: " + i + " Size: " + m_size); }
From source file:Main.java
/** * Returns the indexed value into the {@code Iterable}. It first checks to * see if the {@code Iterable} is a {@code List}, and if so calls the get * method. Otherwise, it walks the {@code Iterable} to get to the element. * * @param <DataType>//from w w w . j a v a 2s . co m * The type of data. * @param iterable * The iterable to pull the value from. * @param index * The 0-based index to pull from the iterable. * @return * The value at the given spot in the iterable. * @throws IndexOutOfBoundsException * If the index is less than zero or greater than or equal to the * number of elements in the iterable. */ public static <DataType> DataType getElement(final Iterable<DataType> iterable, int index) { if (iterable instanceof List<?>) { return ((List<DataType>) iterable).get(index); } else { if (index < 0) { // Bad index. throw new IndexOutOfBoundsException("index must be >= 0"); } for (DataType v : iterable) { if (index == 0) { return v; } index--; } // Bad index. throw new IndexOutOfBoundsException("index >= iterable size"); } }
From source file:Main.java
static public int getValidIndex(final List<?> l, final int i, final boolean strict) { final int size = l.size(); if (i > size) { if (strict) throw new IndexOutOfBoundsException("Too high : " + i + " > " + size); return size; } else if (i < -size) { if (strict) throw new IndexOutOfBoundsException("Too low : " + i + " < " + -size); return 0; } else if (i >= 0) { return i; } else {/*from w w w.ja v a2 s. c o m*/ return size + i; } }
From source file:Main.java
/** * makes sense only if iteration order deterministic! *///from w w w. j av a2 s .c o m private static <T> T getElement(final boolean remove, final int index, final Collection<T> coll) { if (index >= coll.size()) throw new IndexOutOfBoundsException(index + " >= " + coll.size()); final Iterator<T> it = coll.iterator(); int i = 0; while (i++ < index) it.next(); final T elem = it.next(); if (remove) it.remove(); return elem; }