Java ArrayList Class

Category: Java   Tags: Java, Java Collection, Java List Interface, Java ArrayList Class

ArrayList class extends AbstractList and implements the List interface. ArrayList has this declaration:

                            public class ArrayList<E>
                            extends AbstractList<E>
                            implements List<E>, RandomAccess, Cloneable, Serializable
                                

Here, E specifies the type of objects that the ArrayList will contain.

Constructors

ArrayList()
It will create an empty list with an initial capacity of 10.
ArrayList(Collection<? extends E> c)
It creates a list containing the elements of the specified collection c. The order will be same as they are returned by the collection's iterator.
ArrayList(int capacity)
Constructs an empty list with the specified initial capacity.

The capacity grows automatically as elements are added to an array list. ArrayList is not synchronized. Therefore if multiple thread is accessing the list then it must synchronized externally. You can check this post about synchronization. You can use Collections.synchronizedList also to synchronize the list:

                                    List list = Collections.synchronizedList(new ArrayList());
                                

The returned iterator of this class are fail-fast i.e during iterating the list if you do any modification, it will throw ConcurrentModificationException exception. Here is one ArrayList example:

JavaArrayListDemo.java

                            package com.tutorial.java.collections;

                            import java.util.ArrayList;
                            import java.util.Iterator;

                            public class JavaArrayListDemo {

                                public static void main(String[] args) {
                                    ArrayList<String> list = new ArrayList<>();
                                    list.add("Apple");
                                    list.add("Orange");
                                    list.add("Mango");

                                    System.out.println("Size: " + list.size());

                                    // Adding element at index 1 i.e after "Apple"
                                    list.add(1, "Guava");
                                    System.out.println("Size: " + list.size());

                                    System.out.println("Showing all elements:");
                                    Iterator<String> iterator = list.iterator();
                                    while (iterator.hasNext()) {
                                        System.out.println(iterator.next());
                                    }
                                }

                            }
                        

Output:

                            Size: 3
                            Size: 4
                            Showing all elements:
                            Apple
                            Guava
                            Orange
                            Mango