How much data can a List can hold at the maximum?
How much data can be added in java.util.List in Java at the maximum?
Is ther开发者_Go百科e any default size of an ArrayList?
It depends on the List
implementation. Since you index arrays with int
s, an ArrayList
can't hold more than Integer.MAX_VALUE
elements. A LinkedList
isn't limited in the same way, though, and can contain any amount of elements.
It would depend on the implementation, but the limit is not defined by the List
interface.
The interface however defines the size()
method, which returns an int
.
Returns the number of elements in this list. If this list contains more than
Integer.MAX_VALUE
elements, returnsInteger.MAX_VALUE
.
So, no limit, but after you reach Integer.MAX_VALUE
, the behaviour of the list changes a bit
ArrayList
(which is tagged) is backed by an array, and is limited to the size of the array - i.e. Integer.MAX_VALUE
How much data can be added in java.util.List in Java at the maximum?
This is very similar to Theoretical limit for number of keys (objects) that can be stored in a HashMap?
The documentation of java.util.List
does not explicitly documented any limit on the maximum number of elements. The documentation of List.toArray
however, states that ...
Return an array containing all of the elements in this list in proper sequence (from first to last element); would have trouble implementing certain methods faithfully, such as
... so strictly speaking it would not be possible to faithfully implement this method if the list had more than 231-1 = 2147483647 elements since that is the largest possible array.
Some will argue that the documentation of size()
...
Returns the number of elements in this list. If this list contains more than
Integer.MAX_VALUE
elements, returnsInteger.MAX_VALUE
.
...indicates that there is no upper limit, but this view leads to numerous inconsistencies. See this bug report.
Is there any default size an array list?
If you're referring to ArrayList
then I'd say that the default size is 0. The default capacity however (the number of elements you can insert, without forcing the list to reallocate memory) is 10. See the documentation of the default constructor.
The size limit of ArrayList
is Integer.MAX_VALUE
since it's backed by an ordinary array.
java.util.List
is an interface. How much data a list can hold is dependant on the specific implementation of List you choose to use.
Generally, a List implementation can hold any number of items (If you use an indexed List, it may be limited to Integer.MAX_VALUE
or Long.MAX_VALUE
). As long as you don't run out of memory, the List doesn't become "full" or anything.
As much as your available memory will allow. There's no size limit except for the heap.
The interface however defines the size() method, which returns an int.
Returns the number of elements in this list. If this list contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
So, no limit, but after you reach Integer.MAX_VALUE, the behaviour of the list changes a bit
ArrayList (which is tagged) is backed by an array, and is limited to the size of the array - i.e. Integer.MAX_VALUE
see the code below of arraylist default it is 10 when u create List l = new ArrayList();
public class ArrayList<E> extends AbstractList<E> implements List<E>,
Cloneable, Serializable, RandomAccess {
private static final long serialVersionUID = 8683452581122892189L;
private transient int firstIndex;
private transient int lastIndex;
private transient E[] array;
/**
* Constructs a new instance of {@code ArrayList} with ten capacity.
*/
public ArrayList() {
this(10);
}
Numbering an items in the java array should start from zero. This was i think we can have access to Integer.MAX_VALUE+1 an items.
精彩评论