Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default size of ArrayList

Looking through some piece of code I noticed one strange initialization of ArrayList:

... = new ArrayList<..>(0);

I've opened JavaSE 7 sources and saw that inner elementData arrray is initialized by empty array constant - {}. When we pass capacity to the ArrayList constructor we do almost the same - new Object[0]. So my question is: is there is any difference between new ArrayList(0) and new ArrayList() ? Shouldn't ArrayList set the default capacity size to smth like 10 ?

Thanks all for answers.

like image 534
Dedyshka Avatar asked Feb 08 '16 11:02

Dedyshka


People also ask

How do I find the default size of an ArrayList?

The size of an ArrayList can be obtained by using the java. util. ArrayList. size() method as it returns the number of elements in the ArrayList i.e. the size.

What is the default value of ArrayList?

ArrayLists have no default values. If you give the ArrayList a initialCapacity at initialization, you're just giving a hint for the size of the underlying array—but you can't actually access the values in the ArrayList until you add the items yourself.

What is the default size of ArrayList and Hashmap in Java?

I know that the default capacity of Vector class in java is 10 and similarly ArrayList also have it's default capacity 10.

Does ArrayList size start at 0 or 1?

The ArrayList index starts at 0 just like arrays, but instead of using the square brackets [] to access elements, you use the get(index) to get the value at the index and set(index,value) to set the element at an index to a new value.


2 Answers

It is necessary to clarify two definitions:

The size represent the number of elements in the list.

The capacity represent the length of the internal array. In other words, the length contains the number of places to put an element;

Exemple of the length of an array list size = 2

When you create one list with the default constructor of Arraylist:

Ex:

List<String> list = new ArrayList<>();
System.out.println(l.size());

The output is : 0

Why?

Because the list is empty, this is explained by the internal array of the list, when you use the default constructor of ArrayList, the internal array is :

private static final Object[] EMPTY_ELEMENTDATA = {}; 

So, an empty array of Objects where the value of the length is 0 (Capacity). As the list is empty the size is 0.

state of the list when the default constructor is used (id of elementData=27)

When you add one element to the list, the size of your list is incremented to 1, and the initial internal array of your list is changed by another array with a length of 10 (capacity); (id of elementData=30)

private static final int DEFAULT_CAPACITY = 10

State of the list after add one element (id of elementData=30)

About the default constructor of ArrayList, the API java says : API Java : https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#ArrayList%28%29

public ArrayList()

Constructs an empty list with an initial capacity of ten.

In other words, this constructor create an empty list (size equals to 0) with an initial capacity of ten elements.(After add one element to the list, the capacity of the list become 10)

When you know the size of your list, you should create your list with this size. Why? because the complexity of the method add (E element) is O(1) amortized (when there is available space in your list), but O(n) (worst-case) if you add more elements than the initial capacity of your list, in this case, a new array (1.5 times the size) is allocated, and the old array is copied to the new one. Clearly, this operation has costs in terms of performance and memory resources.

So, create a list with an initial capacity of 0 has no sense. If you don't know the initial capacity of your list, use the default constructor that give to you one list with an initial capacity of ten elements.

Keep in mind that definitions of size and capacity around ArrayList are differents:

From book Core Java 2: Fundamentals

Allocating an array list as new ArrayList <'Employee>(100) // capacity is 100

is not the same as allocating a new array as new Employee[100] // size is 100

There is an important distinction between the capacity of an array list and the size of an array. If you allocate an array with 100 entries, then the array has 100 slots, ready for use. An array list with a capacity of 100 elements has the potential of holding 100 elements (and, in fact, more than 100, at the cost of additional reallocations); but at the beginning, even after its initial construction, an array list holds no elements at all.

like image 134
Jesus Zavarce Avatar answered Sep 21 '22 22:09

Jesus Zavarce


An ArrayList has an internal array to store the list elements.

There is a difference between the two constructor calls in Java 7 and 8:

If you do new ArrayList<>(0) the ArrayList creates a new Object array of size 0.

If you do new ArrayList<>() the ArrayList uses a static empty Object array of size 0 and switches to an own private array once you add items to the list.

EDIT:

The Javadoc of the default ArrayList constructor seems to contradict this.

/**
 * Constructs an empty list with an initial capacity of ten.
 */
public ArrayList() {
    super();
    this.elementData = EMPTY_DEFAULTCAPACITY_EMPTY_ELEMENTDATA; // = static, empty
}

But it does not create an element array of length 10 immediately, but instead when you add elements or ensure the capacity:

public void ensureCapacity(int minCapacity) {
    int minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA)
        // any size if not default element table
        ? 0
        // larger than default for default empty table. It's already
        // supposed to be at default size.
        : DEFAULT_CAPACITY; // = 10

    if (minCapacity > minExpand) {
        ensureExplicitCapacity(minCapacity);
    }
}
like image 26
wero Avatar answered Sep 20 '22 22:09

wero