List of usage examples for java.lang IndexOutOfBoundsException IndexOutOfBoundsException
public IndexOutOfBoundsException(int index)
From source file:IntVector.java
public int get(final int index) { if (index > m_size - 1) throw new IndexOutOfBoundsException("get[" + index + "] on vector of size " + m_size); return m_values[index]; }
From source file:Main.java
/** * Removes and 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 remove method. Otherwise, it walks the {@code Iterable} to * get to the element and remove it. This only works on {@code Iterable}s * that are {@code List}s or whose {@code Iterator} implements the optional * {@code remove} method./*from ww w . j ava 2 s .c o m*/ * * @param <DataType> * The type of data. * @param iterable * The iterable to remove the value from. * @param index * The 0-based index to remove from the iterable. * @return * The value removed from the given index 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. * @throws UnsupportedOperationException * If the iterable does not support remove. */ public static <DataType> DataType removeElement(final Iterable<DataType> iterable, int index) { if (iterable instanceof List<?>) { return ((List<DataType>) iterable).remove(index); } else { if (index < 0) { // Bad index. throw new IndexOutOfBoundsException("index must be >= 0"); } Iterator<DataType> iterator = iterable.iterator(); while (iterator.hasNext()) { DataType value = iterator.next(); if (index == 0) { iterator.remove(); return value; } index--; } // Bad index. throw new IndexOutOfBoundsException("index >= iterable size"); } }
From source file:Main.java
/** * Returns the <code>index</code>-th value in {@link Iterator}, throwing * <code>IndexOutOfBoundsException</code> if there is no such element. * <p>/* w w w. ja v a 2s . co m*/ * The Iterator is advanced to <code>index</code> (or to the end, if * <code>index</code> exceeds the number of entries) as a side effect of this method. * * @param iterator the iterator to get a value from * @param index the index to get * @param <T> the type of object in the {@link Iterator} * @return the object at the specified index * @throws IndexOutOfBoundsException if the index is invalid * @throws IllegalArgumentException if the object type is invalid */ public static <T> T get(final Iterator<T> iterator, final int index) { int i = index; checkIndexBounds(i); while (iterator.hasNext()) { i--; if (i == -1) { return iterator.next(); } iterator.next(); } throw new IndexOutOfBoundsException("Entry does not exist: " + i); }
From source file:org.terasoluna.gfw.functionaltest.app.exceptionhandling.ThrowIndexOutOfBoundsExceptionFilter.java
@Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { throw new IndexOutOfBoundsException("4_3 Error"); }
From source file:Main.java
/** * Parse a date from ISO-8601 formatted string. It expects a format yyyy-MM-ddThh:mm:ss[.sss][Z|[+-]hh:mm] * * @param date ISO string to parse in the appropriate format. * @return the parsed date// w w w . ja v a2s.c om * @throws IllegalArgumentException if the date is not in the appropriate format */ public static Date parse(String date) { try { int offset = 0; // extract year int year = parseInt(date, offset, offset += 4); checkOffset(date, offset, '-'); // extract month int month = parseInt(date, offset += 1, offset += 2); checkOffset(date, offset, '-'); // extract day int day = parseInt(date, offset += 1, offset += 2); checkOffset(date, offset, 'T'); // extract hours, minutes, seconds and milliseconds int hour = parseInt(date, offset += 1, offset += 2); checkOffset(date, offset, ':'); int minutes = parseInt(date, offset += 1, offset += 2); checkOffset(date, offset, ':'); int seconds = parseInt(date, offset += 1, offset += 2); // milliseconds can be optional in the format int milliseconds = 0; // always use 0 otherwise returned date will include millis of current time if (date.charAt(offset) == '.') { checkOffset(date, offset, '.'); milliseconds = parseInt(date, offset += 1, offset += 3); } // extract timezone String timezoneId; char timezoneIndicator = date.charAt(offset); if (timezoneIndicator == '+' || timezoneIndicator == '-') { timezoneId = GMT_ID + date.substring(offset); } else if (timezoneIndicator == 'Z') { timezoneId = GMT_ID; } else { throw new IndexOutOfBoundsException("Invalid time zone indicator " + timezoneIndicator); } TimeZone timezone = TimeZone.getTimeZone(timezoneId); if (!timezone.getID().equals(timezoneId)) { throw new IndexOutOfBoundsException(); } Calendar calendar = new GregorianCalendar(timezone); calendar.setLenient(false); calendar.set(Calendar.YEAR, year); calendar.set(Calendar.MONTH, month - 1); calendar.set(Calendar.DAY_OF_MONTH, day); calendar.set(Calendar.HOUR_OF_DAY, hour); calendar.set(Calendar.MINUTE, minutes); calendar.set(Calendar.SECOND, seconds); calendar.set(Calendar.MILLISECOND, milliseconds); return calendar.getTime(); } catch (IndexOutOfBoundsException e) { throw new IllegalArgumentException("Failed to parse date " + date, e); } catch (NumberFormatException e) { throw new IllegalArgumentException("Failed to parse date " + date, e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Failed to parse date " + date, e); } }
From source file:Main.java
/** * <p>/* w w w.jav a2s . co m*/ * Removes the element at the specified position from the specified array. All subsequent elements * are shifted to the left (substracts one from their indices). * </p> * * <p> * This method returns a new array with the same elements of the input array except the element on * the specified position. The component type of the returned array is always the same as that of * the input array. * </p> * * <p> * If the input array is <code>null</code>, an IndexOutOfBoundsException will be thrown, because * in that case no valid index can be specified. * </p> * * @param array * the array to remove the element from, may not be <code>null</code> * @param index * the position of the element to be removed * @return A new array containing the existing elements except the element at the specified * position. * @throws IndexOutOfBoundsException * if the index is out of range (index < 0 || index >= array.length), or if the array is * <code>null</code>. * @since 2.1 */ public static <T> T[] remove(final T[] array, final int index) { int length = getLength(array); if (index < 0 || index >= length) { throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length); } Object result = Array.newInstance(array.getClass().getComponentType(), length - 1); System.arraycopy(array, 0, result, 0, index); if (index < length - 1) { System.arraycopy(array, index + 1, result, index, length - index - 1); } return (T[]) result; }
From source file:Main.java
public static void toggleShortEndian(byte[] b, int off, int len) { if (b == null || len == 0) return;/* w w w.j ava 2 s. c o m*/ int end = off + len; if (off < 0 || len < 0 || end > b.length) throw new IndexOutOfBoundsException("b.length = " + b.length + ", off = " + off + ", len = " + len); if ((len & 1) != 0) throw new IllegalArgumentException("len = " + len); byte tmp; for (int i = off; i < end; i++, i++) { tmp = b[i]; b[i] = b[i + 1]; b[i + 1] = tmp; } }
From source file:Main.java
public static void toggleIntEndian(byte[] b, int off, int len) { if (b == null || len == 0) return;// ww w.j av a2 s.c o m int end = off + len; if (off < 0 || len < 0 || end > b.length) throw new IndexOutOfBoundsException("b.length = " + b.length + ", off = " + off + ", len = " + len); if ((len & 3) != 0) throw new IllegalArgumentException("len = " + len); byte tmp; for (int i = off; i < end; i++, i++, i++, i++) { tmp = b[i]; b[i] = b[i + 3]; b[i + 3] = tmp; tmp = b[i + 1]; b[i + 1] = b[i + 2]; b[i + 2] = tmp; } }
From source file:me.st28.flexseries.flexcore.util.MapUtils.java
/** * Retrieves an entry from a map with a given index. Intended for maps that preserve ordering (ex. LinkedHashMap) * * @param map The map to retrieve the entry from. * @param index The index of the entry in the map. * @return The entry at the given index in the given map. *///w w w . j a v a 2 s .com public static <K, V> Entry<K, V> getEntryByIndex(Map<K, V> map, int index) { Validate.notNull(map, "Map cannot be null."); int mapSize = map.size(); int curIndex = 0; Iterator<Entry<K, V>> iterator = map.entrySet().iterator(); while (iterator.hasNext() && curIndex < mapSize) { if (curIndex++ == index) { return iterator.next(); } } throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + mapSize); }
From source file:Main.java
public static void toggleLongEndian(byte[] b, int off, int len) { if (b == null || len == 0) return;/*from w ww. ja v a 2s . c om*/ int end = off + len; if (off < 0 || len < 0 || end > b.length) throw new IndexOutOfBoundsException("b.length = " + b.length + ", off = " + off + ", len = " + len); if ((len & 7) != 0) throw new IllegalArgumentException("len = " + len); byte tmp; for (int i = off; i < end; i += 8) { tmp = b[i]; b[i] = b[i + 7]; b[i + 7] = tmp; tmp = b[i + 1]; b[i + 1] = b[i + 6]; b[i + 6] = tmp; tmp = b[i + 2]; b[i + 2] = b[i + 5]; b[i + 5] = tmp; tmp = b[i + 3]; b[i + 3] = b[i + 4]; b[i + 4] = tmp; } }