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 ?
Best Answer
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 anArrayList
. Period. The specifics of what the maximum size of thearray
orArrayList
differe based upon the implementation of the JVM (which may be lower than theMAX_INT
value). You can't make anArrayList
(or for that matter anint[]
array) that has along
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 aNoSuchElementException
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.