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.
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.
- 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:
Hello Geeks For Geeks
10 20 25 30
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- AbstractList add(int index, E element) method in Java with Examples
- List remove(int index) method in Java with Examples
- Find the index of an array element in Java
- Remove an Element at specific index from an Array in Java
- ArrayList get(index) method in Java with examples
- How to add element at first and last position of linked list in Java?
- Java Program to Get the First and the Last Element of a Linked List
- Java Program to Search an Element in a Linked List
- Java Program to Search an Element in a Circular Linked List
- Program to convert List of Integer to List of String in Java
- Program to convert List of String to List of Integer in Java
- How to iterate over a 2D list (list of lists) in Java
- Replace a character at a specific index in a String in Java
- Java Program to find the Last Index of a Particular Word in a String
- Java Program to Determine the Unicode Code Point at Given Index in String
- Maximum element in an array such that its previous and next element product is maximum
- ArrayDeque element() Method in Java
- Deque element() method in Java
- Queue element() method in Java
- LinkedList element() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.