简体   繁体   中英

Is LinkedList breaks contract if amount of elements in list greater than Integer.MAX_VALUE?

java doc for java.util.List#size()

Returns the number of elements in this list. If this list contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

Research java.util.LinkedList source code:

public method:

public boolean add(E e) {
    addBefore(e, header);
        return true;
    }

private Entry<E> addBefore(E e, Entry<E> entry) {
    Entry<E> newEntry = new Entry<E>(e, entry, entry.previous);
    newEntry.previous.next = newEntry;
    newEntry.next.previous = newEntry;
    size++;
    modCount++;
    return newEntry;
 }

Thus if before adding element size equals Integer.MAX_VALUE size will become -Integer.MAX_VALUE-1

method size just returns field value without checks:

 public int size() {
    return size;
 }

What do you think about it?

在您设法将超过 20 亿个对象放入列表之前很久,您的内存就会耗尽,因此无需担心。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM