I was thinking about it. You may have access to endless memory (one computer with lot of RAM) so that you can keep on adding more and more elements to your ArrayList.
But, I think that you can only access elements BY INDEX up to a certain value of index. The maximum size of this index could be = maximum number your computer can represent, that is long 9,223,372,036,854,775,807 in Java.
I know that you can access the NEXT element using an iterator. But, how do i access elements after the (9,223,372,036,854,775,807)TH index, using an index number ?
2
ArrayList
in Java has a get(int index) method. int
is a signed 32 bit value, with a maximum value of 2,147,483,647. That is the largest possible value that can be accessed in an ArrayList
. Period. The specifics of what the maximum size of the array
or ArrayList
differe based upon the implementation of the JVM (which may be lower than the MAX_INT
value). You can’t make an ArrayList
(or for that matter an int[]
array) that has a long
for its index.
If you were to try to instantiate an array list of this magnitude, you would have a structure that is at least 8 gigabytes – this only accounts for MAX_INT pointers, and not the additional space of the data at each point.
Attempting to access beyond the maximum value allowed through an iterator associated with the array would likely result in one of OutOfMemoryException
, IndexOutOfBoundsException
or a NoSuchElementException
depending on implementation.
This is very impractical use of memory. If one was to want such a data structure, one should investigate less RAM intensive approaches such as databases, sparse arrays, and the like.
4
For normal arrays, if you are using the stock Oracle JVM, it seems that the actual answer is MAX_INT-5
or MAX_INT-2
depending on version.
For ArrayList
the answer seems to be MAX_INT-8
. (Line 191, as the source code apparently has no anchors to link to!)