letter Q
letter Q

Reputation: 15375

Declaring an ArrayList

I was wondering when I instantiate an ArrayList of a class. Is it somehow faster if I declare it like #2 opposed to #1 by giving it an initial size? Or should I only give the constructor an initial size if I know exactly how many columns I will add to it?

  1. List<Column> columns = new ArrayList<Column>();
  2. List<Column> columns = new ArrayList<Column>(10);

Upvotes: 1

Views: 451

Answers (4)

Fritz
Fritz

Reputation: 10045

For this aprticular case, both options have the same results. However, should we rise the bar and take it to a generic case as you ask:

Or should I only give the constructor an initial size if I know exactly how many columns I will add to it?

You could do that to avoid the resizing that happens when you add an element that would be out of the bounds of the list's underlying array.

Check ArrayList's code:

public boolean add(E e) {
    ensureCapacity(size + 1);  // Increments modCount!!
    elementData[size++] = e;
    return true;
}

ensureCapacity is a method that resized the list's underlying array if it's actually "full":

public void ensureCapacity(int minCapacity) {
    modCount++;
    int oldCapacity = elementData.length;
    if (minCapacity > oldCapacity) {
        Object oldData[] = elementData;
        int newCapacity = (oldCapacity * 3)/2 + 1;
        if (newCapacity < minCapacity)
            newCapacity = minCapacity;
        // minCapacity is usually close to size, so this is a win:
        elementData = Arrays.copyOf(elementData, newCapacity);
    }
}

Setting an initial known capacity prevents this resizing. Still, if you know exacly how many elements will you use, why not an array?

Upvotes: 1

Jigar Joshi
Jigar Joshi

Reputation: 240860

by default it allocates 10 reference so no difference

Witness it from source

public  ArrayList() {
     this(10);
 }

Its usage is to predict approximate number, for example if you think it might take upto around 25 elements and you think it could grow as well, then just define initialCapacity to 25 to avoid array copy which is costlier operation

Upvotes: 3

Rohit Jain
Rohit Jain

Reputation: 213193

As they stand, there is no difference between the two.

The parameter in the constructor is used to specify the initial capacity of the List, which is 10 by default.

Or should I only give the constructor an initial size if I know exactly how many columns I will add to it?

Generally, if you are going to add too many elements in the List frequently, you can give a greater initial capacity, so that the backed array is not re-sized too many times. Well, if you are having a fixed size, which won't change at all, you can simply use an array.

Upvotes: 5

Andy Thomas
Andy Thomas

Reputation: 86381

If you know exactly how many members you will put into the ArrayList, then providing the initial capacity (the second case) can avoid re-creating and copying the underlying array representation as the list grows.

Your example uses 10 members, which happens to be the default size of the current implementation of ArrayList. However, I presume you're asking about the more general case.

Upvotes: 0

Related Questions