Hari kanna
Hari kanna

Reputation: 2521

What is the difference between ArrayList al = new ArrayList(); and ArrayList al = new ArrayList(0)?

What is the difference between ArrayList al = new ArrayList(); and ArrayList al = new ArrayList(0)?

Upvotes: 1

Views: 8421

Answers (3)

Angel.King.47
Angel.King.47

Reputation: 7994

ArrayList(0) 

Empty list with the specified initial capacity. Hense none for 0

ArrayList() 

Empty list with an initial capacity of ten.

Please read the following: http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

Upvotes: 6

robor
robor

Reputation: 3089

new ArrayList() gives you an array list with default initial capacity (how much memory is initially allocated from the ArrayList). new ArrayList(0) gives you an array list with zero initial capacity. As soon as an element is added to the list, capacity is allocated.

Upvotes: 1

Abimaran Kugathasan
Abimaran Kugathasan

Reputation: 32468

If you look on the API, it says, ArrayList() - Constructs an empty list with an initial capacity of ten.

ArrayList(int initialCapacity) - Constructs an empty list with the specified initial capacity.

Upvotes: 5

Related Questions