How to Add an Element at Particular Index in Java ArrayList? Last Updated : 31 Mar, 2023 Comments Improve Suggest changes Like Article Like Report ArrayList.add() method is used to add an element at particular index in Java ArrayList. Syntax: public void add(int index, Object element) ; Parameters: index -position at which the element has to be inserted. The index is zero-based.element - the element to be inserted at the specified position. Exception: throws IndexOutOfBoundsException which occurs when the index is trying to be accessed which isn't there in the allocated memory block. In java, this exception is thrown when a negative index is accessed or an index of memory space. Here particularly when an index greater than the size of ArrayList is trying to be fetched or the insertion of an element at an index greater than size() of ArrayList is fetched. Example: For a list of string list=[A,B,C] list.add(1,"D"); list.add(2,"E"); list=[A,D,E,B,C] For a list of integers LIST=[1,2,3] list.add(2,4); list=[1,2,4,3] Implementation: Java // Adding an Element at Particular // Index in Java ArrayList import java.io.*; import java.util.ArrayList; class GFG { // Main driver method public static void main(String[] args) { // Creating an ArrayList ArrayList<String> list = new ArrayList<>(); // Adding elements to ArrayList // using add method for String ArrayList list.add("A"); list.add("B"); list.add("C"); /* Index is zero based */ // 3 gets added to the 1st position list.add(1, "D"); // 4 gets added to the 2nd(position) list.add(2, "E"); // Displaying elements in ArrayList System.out.println(list); } } Output[A, D, E, B, C] Time Complexity: O(n)Auxiliary Space: O(1) As constant extra space is used. Comment More infoAdvertise with us Next Article How to Add an Element at Particular Index in Java ArrayList? M mharshita31 Follow Improve Article Tags : Java Java Programs Java-ArrayList Practice Tags : Java Similar Reads How to Swap Two Elements in an ArrayList in Java? We can swap two elements of Array List using Collections.swap() method. This method accepts three arguments. The first argument is the ArrayList and the other two arguments are the indices of the elements. This method returns nothing. Syntax: public static void swap(List list, int a, int b); Parame 2 min read How to Add Element in Java ArrayList? Java ArrayList class uses a dynamic array for storing the elements. It is like an array, but there is no size limit. We can add or remove elements anytime. So, it is much more flexible than the traditional array. Element can be added in Java ArrayList using add() method of java.util.ArrayList class. 2 min read How to Replace a Element in Java ArrayList? To replace an element in Java ArrayList, set() method of java.util. An ArrayList class can be used. The set() method takes two parameters the indexes of the element that has to be replaced and the new element. The index of an ArrayList is zero-based. So, to replace the first element, 0 should be the 2 min read How to Copy and Add all List Elements to an Empty ArrayList in Java? We can copy and add List items in Array List using addAll() method. This method accepts a Collection (Ex. List) as an argument and adds the collection items at the end of its calling Collection (Ex. ArrayList). This method returns a boolean value. addAll() return true if the collection successfully 2 min read Removing Element from the Specified Index in Java ArrayList The remove(int index) method present in java.util.ArrayList class removes the element at the specified position in this list and shifts any subsequent elements to the left (i.e. subtracts one from their indices). Syntax : public removed_element remove(int index) Parameters: The index of the element 3 min read How to Replace an Element at a Specific Index of the Vector in Java? The Vector class implements a growable array of objects. Vectors basically fall in legacy classes but now it is fully compatible with collections. It is found in the java.util package and implements the List interface, so we can use all the methods of List interface here. Examples Input : Vector= [" 2 min read Sort an Array and Insert an Element Inside Array in Java Sorting an array can be done by using inbuilt sort function while for the insertion we have to create a new array to do so as arrays in Java are immutable. To learn more about sorting in Java follow the article mentioned below: Sorting: Arrays.sort() in Java with examples Approach 1: Create a new ar 3 min read How to Insert all the Collection Elements to the Specified Position in Java ArrayList? The element can be inserted at the collection elements to the specified position in ArrayList using Collection.addAll() method which is present in java.util.ArrayList class. If any element present at the index then that element and all its right side elements are shifted to the right side. this meth 3 min read How to Declare an ArrayList with Values in Java? ArrayList is simply known as a resizable array. Declaring an ArrayList with values is a basic task that involves the initialization of a list with specific elements. It is said to be dynamic as the size of it can be changed. Proceeding with the declaration of ArrayList, we have to be aware of the co 2 min read How Objects Can an ArrayList Hold in Java? ArrayList is a part of the collection framework and is present in java.util package. It provides us with dynamic arrays in Java just as Vector in C++. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. In order to understan 3 min read Like