How to add element at specified index in ArrayList

In this post, I will be sharing how to add an element at the specified index in ArrayList with examples. I have already shared how to add an element at the end of the list in java by using add() method. There is another variant of add(index, element) which adds an element at the specified index.

Read Also: How to Print ArrayList in Java

According to Java docs, It inserts the specified element at the specified position in the ArrayList. Also, it shifts the element at that position (if any) and any subsequent elements to the right i.e. index+1.

Syntax:


The syntax for the method is:

 public void add(int index, Object element)


Parameters:


index: The position at which the element needs to be inserted in the ArrayList. The index is zero-based i.e. the 1st position represents the 0th index, the 2nd position represents the 1st index, the 3rd position represents the 2nd index, and so on.

element: It represents the value that needs to be inserted at the specified position.

Exception:


add(index, element) method throws IndexOutOfBoundsException if the index is out of range i.e. index < 0 or index > size() . You can find out about the exception in detail here.

How to add an element at the specified index in ArrayList

1. Add String elements at the specified index in ArrayList


In the below example, we have created an ArrayList of type String and added elements to it by using add(element) method. After that, we are adding two elements "San Francisco" and "Texas" at the 2nd index and 0th index respectively by using the add(index, element) method.

 import java.util.*;
public class AddArrayListExample {
    public static void main(String args[]) { 
 
       // Declaration of String ArrayList
       ArrayList<String> al = new ArrayList<String>(); 
 
       /* Simple add() method for adding element
          at the end of the ArrayList */ 
       al.add("California");
       al.add("Boston");
       al.add("San jose");
       al.add("New York");
 
       //Adding element to the 3rd position
       //3rd position = 2 index as index starts with 0
       al.add(2,"San Francisco");
       System.out.println("ArrayList after adding String San Francisco:"+ al);
        
       //Addition of String element at 1st position
       al.add(0, "Texas");

       //Displaying the ArrayList
       System.out.println("ArrayList after adding String Texas:"+ al);
    }
}


Output:
ArrayList after adding String San Francisco:[California, Boston, San Francisco, San jose, New York]
ArrayList after adding String Texas:[Texas, California, Boston, San Francisco, San jose, New York]


2. Add Integer elements at the specified index in ArrayList


In the below example, we have created an ArrayList of type Integer and added elements to it by using add(element) method. After that, we are adding two elements 36 and 48 at the 3rd index and 0th index respectively by using the add(index, element) method.

 import java.util.*;
public class AddArrayListExample2 {
    public static void main(String args[]) { 
 
       // Declaration of Integer ArrayList
       ArrayList<Integer> al2 = new ArrayList<>(); 
 
       /* Simple add() method for adding element
          at the end of the ArrayList */ 
       al2.add(12);
       al2.add(22);
       al2.add(32);
       al2.add(42);
 
       //Adding element to the 4th position
       //4th position = 3rd index as index starts with 0
       al2.add(3, 36);
       System.out.println("ArrayList after adding Integer 36: "+ al2);
        
       //Addition of Integer element at 1st position i.e. 0th index
       al2.add(0, 48);

       //Displaying the ArrayList
       System.out.println("ArrayList after adding Integer 48: "+ al2);
    }
}


Output:
ArrayList after adding Integer 36: [12, 22, 32, 36, 42]
ArrayList after adding Integer 48: [48, 12, 22, 32, 36, 42]


That's all for today. Please mention in the comments if you have any questions related to how to add an element at the specified index in ArrayList with examples.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry