0

What is the maximum number of elements an ArrayList of Points can store?

In other words, given this code:

ArrayList<Point> x = new ArrayList<>();
for (int i = 0; i < maxElements; i++) {
    x.add(new Point(0, 0));
}

what is the maximum allowed value for maxElements (given enough heap space) such that x.get(0) is the correct value and is accessible?

Bryan Ho
  • 13
  • 1
  • 5
  • explained better [Here](http://programmers.stackexchange.com/questions/190954/what-is-the-maximum-value-of-index-of-an-arraylist) – Adi Aug 29 '14 at 11:27
  • possible duplicate of [How to get the capacity of the ArrayList in Java?](http://stackoverflow.com/questions/2497063/how-to-get-the-capacity-of-the-arraylist-in-java) – lxcky Aug 29 '14 at 11:27
  • http://stackoverflow.com/questions/4739947/what-is-the-memory-size-of-a-arraylist-in-java – Marcin Mikołajczyk Aug 29 '14 at 11:28
  • 2
    In theory, the maximum number of elements an `ArrayList` can have is limited to `Integer.MAX_VALUE`, but I believe it's slightly shorter then that. Having said that, you are likely to run out memory before you get to that point. The maximum number of values is limited by two factors, the fact that the `ArrayList` is backed by an array and the fact that the API only allows you to pass an `int` value to the `get` method... – MadProgrammer Aug 29 '14 at 11:32
  • I would say the API restriction is the more important one; theoretically `ArrayList` could use more than one array for storage. – Marko Topolnik Aug 29 '14 at 11:35
  • You could still reach elements with an iterator in that case – Absurd-Mind Aug 29 '14 at 11:39
  • Correct me if I am wrong, `1 GiB = 1073741824 bytes (= 1024^3 B = 2^30 B)` and `Integer.MAXVALUE= (2^31)-1` which means leaving apart all other data of loading classes,supporting data for Arraylists etc etc, just the number of that many ArrayList elements will consume 2 GB of RAM ? – Mustafa sabir Aug 29 '14 at 11:42
  • How can you add a `Point` to an `ArrayList`? It is invalid syntax to use anything but the **exact** class name in generic declarations - change it to `ArrayList`. – bcsb1001 Aug 29 '14 at 12:38
  • 1
    @Mustafasabir I would say 8GB only for the references (4Byte per reference) of all the Points – Absurd-Mind Aug 29 '14 at 12:39

4 Answers4

2
Integer.MAX_VALUE - 8

Since this is the maximum size of an ArrayList

Source: Line 191

Absurd-Mind
  • 7,884
  • 5
  • 35
  • 47
0

The maxElements could be a limit for the list. You can use x.size() > 0 check if you want x.get(0).

ArrayList<Points> x = new ArrayList<>();
for (int i = 0; i < maxElements; i++) {
    x.add(new Point(0, 0));
}
Points p;
if (x.size() > 0)
  p = x.get(0);
0
What is the maximum number of elements an ArrayList of Points can store?

ArrayList and Points are Objects and are stored in heap memory.If the heap memory can support,it can store a maximum of Integer.MAX_VALUE(2^31-1) but you may get java.lang.OutOfMemoryError before that.

What is the value of maxElements such that x.get(0) is the correct value and is accessible?

maxElements can have any value grater than equal to 1(so that it contains at least 1 element)

Kumar Abhinav
  • 6,565
  • 2
  • 24
  • 35
  • Clearly, OP wanted to ask "what is the *maximum* value of `maxElements` such that ...". – Marko Topolnik Aug 29 '14 at 11:35
  • @MarkoTopolnik op asked 2 questions..First and last statement – Kumar Abhinav Aug 29 '14 at 11:36
  • And my comment is targetted at his second question, pointing out the correct interpretation of that question. In fact, OP has only one question which he asked in two ways, hoping the second way would add more strictness. – Marko Topolnik Aug 29 '14 at 11:37
  • I said "OP *wanted* to ask". It was quite obvious from the context. His second question starts with "In other words," which should have given you a clue. – Marko Topolnik Aug 29 '14 at 11:41
0

Theoretically is 2^31, but practically it is depend on how mach memory do you have.

talex
  • 17,973
  • 3
  • 29
  • 66