ArrayList in Java

ArrayList in Java is one of the most widely used collection classes.

ArrayList in Java is a resizable-array implementation of the List interface. ArrayList in Java implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list.

The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed. Standard Java arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you must know in advance how many elements an array will hold. Hence, ArrayList is a very good alternative for traditional arrays.

An ArrayList is a variable-length array of object references. That is, an ArrayList can dynamically increase or decrease in size. Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically.

ArrayList is similar to Vector, except that ArrayList is unsynchronized.

Some of the important points about ArrayList:

  • ArrayList is not synchronized.
  • ArrayList can contain duplicate elements.
  • ArrayList extends AbstractList class and implements List interface.
  • ArrayList maintains insertion order.
  • ArrayList allows random access because array works at the index basis.
  • ArrayList is a member of the Java Collections Framework.

ArrayList with an Example:

The following program shows a simple use of ArrayList.


The capacity of an ArrayList object increases automatically as objects are stored in it, you can increase the capacity of an ArrayList object manually by calling ensureCapacity( ) method provided by ArrayList class.
The signature for ensureCapacity( ) is shown here:

where minCapacity the desired minimum capacity.

Related posts

Leave a Comment