-
Bug
-
Resolution: Fixed
-
P4
-
6
-
b17
-
generic
-
generic
-
Not verified
Arrays.fill() methods that fill an entire array, e.g.,
public static void fill(long[] a, long val) {
fill(a, 0, a.length, val);
}
call the from/to-index form, e.g.,
public static void fill(long[] a, int fromIndex, int toIndex, long val) {
rangeCheck(a.length, fromIndex, toIndex);
for (int i=fromIndex; i<toIndex; i++)
a[i] = val;
}
which call rangeCheck(), vis,
private static void rangeCheck(int arrayLen, int fromIndex, int toIndex) {
if (fromIndex > toIndex)
throw new IllegalArgumentException("fromIndex(" + fromIndex +
") > toIndex(" + toIndex+")");
if (fromIndex < 0)
throw new ArrayIndexOutOfBoundsException(fromIndex);
if (toIndex > arrayLen)
throw new ArrayIndexOutOfBoundsException(toIndex);
}
When filling an entire array, none of the exceptions in rangeCheck can happen,
because fromIndex == 0 and toIndex == arrayLen and is always >= 0.
For these fill() methods, the call to rangeCheck can be elided.
public static void fill(long[] a, long val) {
fill(a, 0, a.length, val);
}
call the from/to-index form, e.g.,
public static void fill(long[] a, int fromIndex, int toIndex, long val) {
rangeCheck(a.length, fromIndex, toIndex);
for (int i=fromIndex; i<toIndex; i++)
a[i] = val;
}
which call rangeCheck(), vis,
private static void rangeCheck(int arrayLen, int fromIndex, int toIndex) {
if (fromIndex > toIndex)
throw new IllegalArgumentException("fromIndex(" + fromIndex +
") > toIndex(" + toIndex+")");
if (fromIndex < 0)
throw new ArrayIndexOutOfBoundsException(fromIndex);
if (toIndex > arrayLen)
throw new ArrayIndexOutOfBoundsException(toIndex);
}
When filling an entire array, none of the exceptions in rangeCheck can happen,
because fromIndex == 0 and toIndex == arrayLen and is always >= 0.
For these fill() methods, the call to rangeCheck can be elided.
- relates to
-
JDK-4809552 Optimize Arrays.fill(...)
- Closed