9

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

2 Answers 2

17

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
  • 2
    -1, you cannot allocate an array of 2^32-1 elements (at least in the most common Java configurations) Commented Mar 18, 2013 at 5:51
  • 2
    @StevenSchlansker I've modified the answer to incopropate your changes. On the machine I was working on at the time, I didn't have the src.jar to be able to dig into and verify... and yes, it is jvm dependent.
    – user40980
    Commented Mar 18, 2013 at 15:44
  • @MichaelT - Can you tell me what to look for in src.jar. I never heard of this file. Are there any other interesting properties stored in this file ? Commented Mar 19, 2013 at 11:14
  • @JediKnight src.jar is the source version of all of the java.* classes that are used on that jvm. There are many interesting properties and code bits stored in there - though its not a small file to dig into. The ArrayList link that Steven posted is one file form the openjdk src.jar (and is different than the one I have on the machine I'm on now).
    – user40980
    Commented Mar 19, 2013 at 14:18
7

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!)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.