ArrayList add Method Example

In this article, we will look at the ArrayList add method. The ArrayList add method is used to add an element in the list. We will look at the different options in the add()method.

1. ArrayList Add() Method

ArrayList provides the options to add any type of element to the list, but it’s always recommended using the defined type while adding elements to the ArrayList. This will also help us use the compile time type safety guarantee while adding the element to array-list. The ArrayList add method in Java takes care of number of things automatically for us.

  • It will make sure that it has sufficient space. Remember ArrayList is a dynamic array.
  • In case there is no sufficient space, it will add more space to the underlying array by creating a new array and copying old array to the new one.
  • Finally, it will add the new element to the list.

Here is the signature for the ArrayList add method in Java. Please remember there are other overloaded add() method.

 public boolean add(E e) {
   modCount++;
   add(e, elementData, size);
   return true;
}
  • e – element to be appended to this list.
  • Returns true if this collection changed because of the call

Above method will add the element to the end of the list.

ArrayList Add Examples

Le’s inspect the different example of adding element to the ArrayList and the different overloaded variations.

2. Add(E e) Method

The default method (used in most cases) add(100) will add the element to the end of the list. Here is a simple code to add element to the ArrayList.

ArrayList<Integer> list=new ArrayList<Integer>();
list.add(100); // it will be added to the end of list if there are already element in the Arraylist.
ArrayList add method

3. Add Element to specific index in ArrayList (add(int index, E element))

We can use the add(int index, E element) method to insert the element at the specified position in this list. This will shift the element at the current position (if any) and any other subsequent element to the right before inserting the element at the specified position. Let see how this work internally.

arraylist add method
List<Integer> list=new ArrayList<Integer>();
list.add(22);
list.add(34);
list.add(2);
list.add(45);
list.add(11);
list.add(23);
list.add(26);

//adding at specific index
list.add(3, 140);

//output
[22, 34, 2, 140, 45, 11, 23, 26]

4. ArrayList Add Method with type-safe

In case you want to make sure that your code works expected in all cases, always use the generic type while defining the ArrayList. The type-safety is a feature provided by compiler to ensure you can only add defined type in the array-list. Let’s look at the following example for better understanding.

 List<Integer> list=new ArrayList<Integer>();
   list.add(22);
   list.add(34);
   list.add(2);
   list.add(45);
   
   //compiler will not allow the following line and give compile type error
   list.add("test");

5. Add Raw Element Type to ArrayList

We can create a raw type (Type Object) ArrayList. This will give us the option to add any element to the ArrayList. Let’s look at the example:

  List list=new ArrayList();
  list.add(22);
  list.add("Hello");
  list.add(30.33);
        
  System.out.println(list);

 //output
[22, Hello, 30.33]

Although we could add the element to the list, there is no way we can get the element from the list as we really not sure about the type of element and without the type, you will end up getting ClassCastException. This is not recommended and should never be used.

Summary

In this article, we talked about the ArrayList add method. We covered the following important points.

  • How the add method works with ArrayList in Java?
  • What are the different way to add element to the ArrayList.
  • ArrayList and Type-Safety check.

Please check the source code for all articles on our GitHub repository.