/** * Checks if the given index is in range. If not, throws an appropriate * runtime exception. This method does *not* check if the index is * negative: It is always used immediately prior to an array access, * which throws an ArrayIndexOutOfBoundsException if index is negative. */ public static void rangeCheck(int index, int size) { if (index >= size) { throw new IndexOutOfBoundsException(outOfBoundsMsg(index, size)); } }
/** * A version of rangeCheck used by add and addAll. * * @param index the accessed index * @param size the size of the container */ public static void rangeCheckForAdd(int index, int size) { if (index > size || index < 0) { throw new IndexOutOfBoundsException(outOfBoundsMsg(index, size)); } }