Java ArrayList is an implementation of the List interface which grows automatically as we add elements to it. Java arrays are fixed in size, that means that the size of an array cannot be changed once it is created, while Java ArrayList can grow and shrink in size as we add or remove elements from it. Java ArrayList uses an array internally to store its elements.

The ArrayList class implements all the optional operations defined by the List interface. It also allows null elements. Like array, elements of an ArrayList can be accessed using index. ArrayList index starts from 0 to ArrayList.size() – 1.

Since ArrayList class implements the RandomAccess interface, its elements can be accessed randomly by specifying the index of it. This operation is a constant time operation.

Length of an internal array maintained by the ArrayList is called capacity of the ArrayList. As you add elements to the ArrayList, the ArrayList capacity grows automatically.

Object of an ArrayList can be created using new keyword. For example,

ArrayList stringList = new ArrayList();

will create an ArrayList of String objects. Please note that primitive values cannot be stored in the ArrayList, only objects can. You can use add method of an ArrayList to add elements to it. For example,


will add “One” to the ArrayList object. To access the elements of an ArrayList, you can use the get method along with the index like,

System.out.println( stringList.get(0) );

will print “One” i.e. the first element of an ArrayList.

Java ArrayList is not synchronized. That means that if multiple threads are modifying the ArrayList, you must synchronize it.

ArrayList elements can also be accessed using Iterator or ListIterator apart from using the element index.

The below given section lists Java ArrayList examples in details.

Java ArrayList Examples

Join 1000+ fellow learners! Enter your email address below: