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 ?
-
See stackoverflow.com/a/5677787/821436Martin Schröder– Martin Schröder2013年03月18日 12:56:20 +00:00Commented Mar 18, 2013 at 12:56
-
4A computer doesn't have "endless memory"; it has, at most, an amount that can be referenced by the memory-address register. So if you have a 64-bit machine, then you can access 2^64-1 bytes of addressable memory.chrisaycock– chrisaycock2013年03月18日 14:59:37 +00:00Commented Mar 18, 2013 at 14:59
2 Answers 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.
-
2-1, you cannot allocate an array of 2^32-1 elements (at least in the most common Java configurations)Steven Schlansker– Steven Schlansker2013年03月18日 05:51:01 +00:00Commented 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– user409802013年03月18日 15:44:44 +00:00Commented 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 ?Jedi Knight– Jedi Knight2013年03月19日 11:14:09 +00:00Commented 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. TheArrayList
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– user409802013年03月19日 14:18:53 +00:00Commented Mar 19, 2013 at 14:18
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!)