List add(int index, E element) method in Java Last Updated : 03 Dec, 2024 Comments Improve Suggest changes 7 Likes Like Report 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. Implementation: Java // Java code to illustrate add(int index, E elements) import java.util.*; public class ArrayListDemo { public static void main(String[] args) { // Creating List List<String> l = new ArrayList<String>(5); // Adding element in List l.add("Geeks"); l.add("For"); l.add("Geeks"); // use add() method to initially // add elements in the list l.add(0, "Hello"); // Printing elements for (String str : l) { System.out.print(str + " "); } } } OutputHello Geeks For Geeks Syntax of Methodpublic 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: Boolean and it returns true if the object is added successfully. 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.Example of List add() MethodBelow programs illustrate the List.add(int index, E element) method: Java // Java code to illustrate add(int index, E elements) import java.util.*; public class ArrayListDemo { public static void main(String[] args) { // Create an empty list List<Integer> l = new ArrayList<Integer>(5); // Adding Elements in List l.add(10); l.add(20); l.add(30); // Add a new 25 at index 2 and print true // if the element is added successfully System.out.println(l.add(2, 25)); // Prints element of List for (Integer num : l) { System.out.print(num + " "); } } } Output10 20 25 30 Create Quiz Comment G gopaldave Follow 7 Improve G gopaldave Follow 7 Improve Article Tags : Java Java-Collections Java-Functions java-list Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References7 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers1 min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like