In the case of an archaist in Java. What is the maximum bandwidth of the list

Now according to SCJP arraist max. the size should be based on the size of available memory, but list.getSize () returns Integer. Therefore, we can safely assume that it INTEGER.MAXSIZEis maximum. array capacity. ie int Maximum allowed value

+4
source share
4 answers

Well, since it is ArrayListsupported by the array, its maximum capacity cannot be higher than the maximum length of the array, which is tied to Integer.MAX_VALUE(since the index of the array is always equal int).

+5
source

, , : Java ?.

:

1.-

2.- Integer

, .

, !

+2

:

- Java, , . - ArrayList, , List .

Arrays vs Arraylist

Arraylist , :

Java 32- ints, 2147483647 .

Well, if your items have a memory capacity large enough, you can either deplete your VM heap size or your computer's memory before you reach that number.

0
source

ArrayList massive capacity - Integer.MAX_VALUE -8 and ArrayList can raise 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 by capacity, but may return an invalid 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++;
}
0
source

Source: https://habr.com/ru/post/1613631/


All Articles