List add(int index, E element) method in Java Last Updated : 03 Dec, 2024 Comments Improve Suggest changes Like Article 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 Comment More infoAdvertise with us G gopaldave Follow Improve Article Tags : Java Java-Collections Java-Functions java-list Practice Tags : JavaJava-Collections Explore BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOPs & InterfacesClasses and Objects in Java10 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 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 Tutorial15+ min readSynchronization in Java10 min readFile Handling in Java5 min readJava Method References9 min readJava 8 Stream Tutorial15+ min readJava Networking15+ min readJDBC Tutorial12 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions7 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like