ArrayList in Java

ArrayList in Java is a dynamic, resizable list of objects and utilizes an array data structure. Java ArrayList is a part of the Collection framework and it implements List Interface.

Internally Java ArrayList uses an Array object to store elements dynamically or we can say Java ArrayList is backed with an array data structure inside which every element is added inside array itself.

Whenever we create ArrayList in Java array of default capacity (10) will be created and elements are added into this array.

1. Features of ArrayList

  • ArrayList is dynamic, i.e elements can be added at runtime.
  • Duplicate values are allowed in ArrayList.
  • ArrayList in non-synchronized i.e operations are fast but not thread-safe.
  • Elements can be accessed using index positions.
  • ArrayList maintains the insertion order of elements.

2. How ArrayList Works Internally?

Java ArrayList is backed with an array that means every element added or remove is actually modified in backing array.

2.1 What is an Array-Backed Data Structure?

Array backed data structures are array-based and use an array as their internal implementation to store and manipulate data.

ArrayList is one of them. Let’s see how ArrayList is implemented in Java?

  public class ArrayList<E> extends AbstractList<E>
      implements List<E>, RandomAccess, Cloneable, java.io.Serializable {

    /** The array buffer into which the elements of the ArrayList are stored. */
    transient Object[] elementData;

    // more code

  }

As you can see, internally array named elementData is used to store elements. 

3. Java ArrayList Methods

MethodDescription
1. add(element)Append element at the end of the list.
2. add(index, element)Append element at the specified index of the list.
3. addAll(collection)Append all elements of the specified collection at the end of the list. Eg: Append two different List
4. remove(index)Remove element from the specified index position.
5. clear()Remove all elements from the list.

These are some commonly used methods of ArrayList.

4. Java ArrayList Programs?

Now, let’s see some example of above-given methods:

4.1 How to create ArrayList?

    //Non-generic Way
    ArrayList list = new ArrayList();
     
    //Generic Way to create ArrayList with default capacity
    List<Integer> number1 = new ArrayList<>(); 
     
    //Create ArrayList with the given capacity
    List<Integer> number2 = new ArrayList<>(5); 

It’s not recommended to create ArrayList using a non-generic way. Always create ArrayList by specifying the data type it’s going to store.

4.2 How to Convert Array to ArrayList?

  public static void main(String[] args) {

    String[] stringArray = { "A", "B", "C", "D", "E" };

    // Method 1
    List<String> stringList = Arrays.asList(stringArray);

    // Method 2
    List<String> stringList2 = new ArrayList<String>();
    Collections.addAll(stringList2, stringArray);
    
}

4.3 How to Sort ArrayList in Java?

  public static void main(String[] args) {

    List<String> stringList = Arrays.asList("E", "D", "C", "B", "A");

    System.out.println("Before Sorting " + stringList);

    Collections.sort(stringList); // sorting arraylist

    System.out.println("After Sorting " + stringList);
}

Output:

Before Sorting [E, D, C, B, A]
After Sorting [A, B, C, D, E]

4.4 How to Iterate ArrayList in Java?

public class JavaCollectionExample {

  public static void main(String[] args) {
    ArrayList<Integer> integerList = new ArrayList<Integer>();
    integerList.add(1);
    integerList.add(3);
    integerList.add(5);
    integerList.add(7);

    /** For Loop for iterating ArrayList */
    for (int i = 0; i < integerList.size(); i++) {
      System.out.println(integerList.get(i));
    }

    /** Advanced For Loop */
    for (Integer num : integerList) {
      System.out.println(num);
    }

    /** Looping Array List using Iterator */
    Iterator<Integer> iterator = integerList.iterator();
    while (iterator.hasNext()) {
      System.out.println(iterator.next());
    }

    /** Using Java 8 */
    integerList.forEach(System.out::println);

  }

}

Above, I showed how you can iterate over ArrayList. Iterating using Java 8 is my favorite part.

I hope you’ve learned about ArrayList in Java.

Happy Learning!

LinkedList in Java

Leave a Comment