List add(int index, E element) method in Java

The add(int index, E ele) method of List interface in Java is used to insert the specified element at the given index in the current list.

Syntax:

public void add(int index, E element)

Parameter: This method accepts two parameters as shown in the above syntax:

  • index: This parameter specifies the index at which we the given element is to be inserted.
  • element: This parameter specifies the element to insert in the list.

Return Value: The return type of the function is void and it does not returns anything.

Exceptions:



  • UnsupportedOperationException – It throws this exception if the add() operation is not supported by this list.
  • ClassCastException – It throws this exception if the class of the specified element prevents it from being added to this list.
  • NullPointerException – It throws this exception if the specified element is null and this list does not permit null elements.
  • IllegalArgumentException – It throws this exception if some property of this element prevents it from being added to this list.

Below programs illustrate the List.add(int index, E element) method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate add(int index, E elements)
import java.io.*;
import java.util.*;
  
public class ArrayListDemo {
    public static void main(String[] args)
    {
  
        // create an empty list with an initial capacity
        List<String> list = new ArrayList<String>(5);
  
        // use add() method to initially
        // add elements in the list
        list.add("Geeks");
        list.add("For");
        list.add("Geeks");
  
        // Add a new element at index 0
        list.add(0, "Hello");
  
        // prints all the elements available in list
        for (String str : list) {
            System.out.print(str + " ");
        }
    }
}

chevron_right


Output:

Hello Geeks For Geeks

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate add(int index, E elements)
import java.io.*;
import java.util.*;
  
public class ArrayListDemo {
    public static void main(String[] args)
    {
  
        // create an empty list with an initial capacity
        List<Integer> list = new ArrayList<Integer>(5);
  
        // use add() method to initially
        // add elements in the list
        list.add(10);
        list.add(20);
        list.add(30);
  
        // Add a new  25 at index 2
        list.add(2, 25);
  
        // prints all the elements available in list
        for (Integer num : list) {
            System.out.print(num + " ");
        }
    }
}

chevron_right


Output:

10 20 25 30

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/List.html#add(int, %20E)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.