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
- 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?
- Deque element() method in Java
- Queue element() method in Java
- ConcurrentLinkedDeque element() method in Java
- LinkedBlockingDeque element() method in Java
- ArrayDeque element() Method in Java
- LinkedList element() method in Java with Examples
- BlockingDeque element() method in java with examples
- AbstractQueue element() method in Java with examples
- List add() 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.