ArrayList in Java

Java ArrayList is a class that implements the List interface. It is based on an array data structure.

We use ArrayList more than traditional arrays because it allows us to create a dynamic collection of data (the length can be changed), while traditional Java arrays have a fixed length.

The biggest advantage of using lists over arrays is that we can change the list size and insert or remove elements at certain indexes, while with arrays, this is not possible.

Also, the ArrayList class enables us to use predefined methods to easily manipulate its elements.

Some key points to remember:

  • The ArrayList class can contain duplicate elements
  • Indexing starts from zero
  • The ArrayList class allows random access at the index level
  • ArrayList does not work with primitive types.

How to create an ArrayList in Java?

We create a new ArrayList object by instantiating it, just like any other class in Java.

 ArrayList<String> list = new ArrayList<>();


Between the <> brackets, we specify which type of elements the list will contain.

We can also create an ArrayList using polymorphism.
The variable that holds the address of the new object will be of type List.
This is possible because ArrayList implements the List interface.

 List<Integer> list = new ArrayList<>();

How to add elements to ArrayList in Java?

We add elements using the add() method that the ArrayList class inherits from the Collection interface.
Example:

class Test {
    
  public static void main(String[] args) {
    List<String> list = new ArrayList<>();
        
    list.add("Steve");
    list.add("Megan");
    list.add("Melissa");
    list.add("Ryan");
      
    System.out.println(list);
  }
}
Output: [Steve, Megan, Melissa, Ryan]
 
Using the addAll() method, we can add all the elements of an existing list to the newly created list.
Example:
class Test {
    
  public static void main(String[] args) {
    List<String> list = new ArrayList<>();
    list.add("Steve");
    list.add("Megan");
    list.add("Melissa");
    list.add("Ryan");
        
    List<String> list2 = new ArrayList<>();
    list2.addAll(list);
        
    System.out.println(list2);    
  }
}
Output: [Steve, Megan, Melissa, Ryan]
 

How to remove elements from an ArrayList?

We can remove list elements in four ways:

By element

We can use the remove(Object o) method and specify the exact element we want to remove from the list.
Example:

class Test {
    
  public static void main(String[] args) {
    List<String> list = new ArrayList<>();
    list.add("Steve");
    list.add("Megan");
    list.add("Melissa");
    list.add("Ryan");
        
    list.remove("Megan");
       
    System.out.println(list);
  }
}
Output: [Steve, Melissa, Ryan]

By index

We can use the remove(int index) method and specify the index of an element we want to remove from the list.
Example:

class Test {
    
  public static void main(String[] args) {
    List<String> list = new ArrayList<>();
        
    list.add("Steve");
    list.add("Megan");
    list.add("Melissa");
    list.add("Ryan");
    
    list.remove(2);
    
    System.out.println(list);
  }
}
Output: [Steve, Megan, Ryan]

Removing all elements that are present in the specified collection

We can use removeAll(Collection<?> c) to remove all elements present in the collection we passed in as a parameter.

class Test {
    
  public static void main(String[] args) {
    List<String> list = new ArrayList<>();
    list.add("Steve");
    list.add("Megan");
        
    List<String> list2 = new ArrayList<>();
    list2.add("Steve");
    list2.add("Megan");
    list2.add("Melissa");
    list2.add("Ryan");
    list2.removeAll(list);
   
    System.out.println(list2);
  }
}
Output: [Melissa, Ryan]

Clear the list / remove all elements from the list

We can use the clear() method to remove all elements from the list.
Example:

class Test {
  public static void main(String[] args) {
    List<String> list = new ArrayList<>();
    list.add("Steve");
    list.add("Megan");
    list.add("Melissa");
    list.add("Ryan");
    list.clear();
    System.out.println(list);
  }
}
Output: []

How to change an element in Java ArrayList?

We can replace the element by using the set(int index, String element) method.
As parameters, we pass the index of the element and the element with which we want to replace it.
Example:

class Test {
    
  public static void main(String[] args) {
    List<String> list = new ArrayList<>();
        
    list.add("Steve");
    list.add("Megan");
    list.add("Melissa");
    list.add("Ryan");
    
    list.set(0, "Elon");
       
    System.out.println(list);
  }
}
Output: [Elon, Megan, Melissa, Ryan]

How to access ArrayList elements in Java?

To access an element of the list, we use the get(int index) method and pass the index of the element we want to fetch.
Example:

class Test {
    
  public static void main(String[] args) {
    List<String> list = new ArrayList<>();
        
    list.add("Steve");
    list.add("Megan");
    list.add("Melissa");
    list.add("Ryan");
       
    System.out.println(list.get(3));
  }
}
Output: Ryan

How to get the size of the Java ArrayList?

To find out the size of the ArrayList in Java, we use the size() method that the ArrayList class inherits from the Collection interface.

Example:

class Test {
    
  public static void main(String[] args) {
    List<String> list = new ArrayList<>();
    list.add("Steve");
    list.add("Megan");
    list.add("Melissa");
    list.add("Ryan");
        
    System.out.println("Size of the list: " + list.size());
  }
}
Output: Size of the list: 4
 
That was all about Java ArrayLists. Proceed to the next lesson to learn more about Java LinkedList.

Happy Learning!

Leave a Reply

Your email address will not be published. Required fields are marked *