4

Now as per SCJP the arraylist max. size should be depending on the size of available memory, but list.getSize() returns an Integer. So is it safe to assume that INTEGER.MAXSIZE is the max. capacity of array . i.e int Max allowed value

Felix Gerber
  • 1,615
  • 3
  • 30
  • 40
SanketRao
  • 41
  • 5

4 Answers4

5

Well, since ArrayList is backed by an array, its max capacity can't be higher than the max length of an array, which is bound by Integer.MAX_VALUE (since the index of an array is always an int).

Eran
  • 387,369
  • 54
  • 702
  • 768
  • According to http://stackoverflow.com/questions/3038392/do-java-arrays-have-a-maximum-size the maximum size of a Java array depends on the VM, and is in some cases less than `Integer.MAX_VALUE`. – sdgfsdh Oct 29 '15 at 10:49
  • 1
    @sdgfsdh Either I misread your comment or you edited it. I thought you were referring to the max size of an ArrayList, not an array. – Eran Oct 29 '15 at 10:52
2

There is another question with an answer matching your question and also an simple example to test: Do Java arrays have a maximum size?.

There are two limits:

1.- The available memory

2.- The max size of an Integer

But im pretty sure you hit the first before the second limit.

Hope that helps!

Community
  • 1
  • 1
Felix Gerber
  • 1,615
  • 3
  • 30
  • 40
  • I think I read about a proposal for `long` indexed arrays, so some people do seem to have enough memory to want such a thing. But, yeah, for most people, the memory limit must be the first wall they run into. – Thilo Oct 29 '15 at 10:58
0

From different sources:

List is an interface in Java, which means that it may have multiple implementations. One of these implementations is ArrayList, which is a class that implements the behavior of the List interface using arrays as the data structure.

Arrays vs Arraylist

And since Arraylist is based on arrays, you max size should be:

Java arrays are accessed via 32-bit ints, resulting in a maximum theoretical array size of 2147483647 elements.

Max array size

Well, If your elements has a memory footprint big enough you could either deplete your VM's heap size or your machines memory before reaching that number, though.

Community
  • 1
  • 1
crigore
  • 400
  • 5
  • 23
0

ArrayList max capasity is Integer.MAX_VALUE -8 and ArrayList can throw OutOfMemoryError

private static int hugeCapacity(int minCapacity) {
    if (minCapacity < 0) // overflow
        throw new OutOfMemoryError();
    return (minCapacity > MAX_ARRAY_SIZE) ?
        Integer.MAX_VALUE :
        MAX_ARRAY_SIZE;
}
 /**
 * The maximum size of array to allocate.
 * Some VMs reserve some header words in an array.
 * Attempts to allocate larger arrays may result in
 * OutOfMemoryError: Requested array size exceeds VM limit
 */
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

LinkedList is not limited in capacity but it can return not valid size

public boolean add(E e) {
    linkLast(e);
    return true;
}
void linkLast(E e) {
    final Node<E> l = last;
    final Node<E> newNode = new Node<>(l, e, null);
    last = newNode;
    if (l == null)
        first = newNode;
    else
        l.next = newNode;
    size++;
    modCount++;
}
Dmitriy Kuzkin
  • 459
  • 4
  • 4