Let’s say I have an array that is long enough to access any of its index with int, is there any way to access the index of such an array with long? And how the Java handle this kind of array? Example:
int[] a = new int[]{1,5,2,4........9,2,1}
Assume in the above array that 9,2,1 are at indices that are beyond the range of int (231).
How would I access these elements?
You wouldn’t – array indexes are always
intvalues in Java. It doesn’t allow for an array with more thanInteger.MAX_VALUEelements.The length of an array is represented by the
lengthfield, which is of typeint. Therefore it is impossible to create an array with a length greater thanInteger.MAX_VALUE.The spec doesn’t explicitly call this out, but you can infer it from the types involved.