ArrayList

ArrayList is a Collection class which extends AbstractList and implements List interface.

  1. ArrayList underlying data structure is dynamic grow-able array.
  2. It implements random access interface. So we can access elements from the array list randomly by using index.
  3. ArrayList is not synchronized. So if we are using ArrayList in multi threading we need to explicitly make ArrayList as synchronized by using Collection.synchronizedList(List l) method.
  4. In ArrayList we can’t be used with primitives with generic.(ArrayList a = new ArrayList<>() : It is invalid and compile time error).

Constructors:

  1. ArrayList(): Creates the object with default initial capacity. (Till Java 1.6 initial capacity is “10” but from java 1.7 initial capacity is “0” but when we add first element then capacity will be “10”).
  2. ArrayList(int initialCapacity): creates the object with specified capacity.
  3. ArrayList(Collection c): Creates the object with specified collection data.

When the capacity increased to max then the new capacity will be

int newCapacity = (oldCapacity * 3)/2;
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s