How to Swap Two Elements in an ArrayList in Java? Last Updated : 01 Jul, 2021 Comments Improve Suggest changes Like Article Like Report 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); Parameters list: An ArrayList or any List implementing class in which elements are swappeda: index of the first elementb: index of the second element Exception: It throws IndexOutOfBoundsException if the index of Array List is less than 0 or greater than the size of the ArrayList. Example 1 Java // Java program to swap two elements in an ArrayList import java.util.ArrayList; import java.util.Collections; public class GFG { public static void main(String a[]) { // Create the Array List ArrayList<String> ArrList = new ArrayList<String>(); // add the values in Array List ArrList.add("Item 1"); ArrList.add("Item 2"); ArrList.add("Item 3"); ArrList.add("Item 4"); ArrList.add("Item 5"); // display Array List before swap System.out.println("Before Swap the ArrayList "); System.out.println(ArrList); // Swapping the elements at 1 and 2 indices Collections.swap(ArrList, 1, 2); // display Array List after swap System.out.println("After Swap the ArrayList"); System.out.println(ArrList); } } OutputBefore Swap the ArrayList [Item 1, Item 2, Item 3, Item 4, Item 5] After Swap the ArrayList [Item 1, Item 3, Item 2, Item 4, Item 5] Example 2 Java // Java program to swap two elements in an ArrayList import java.util.ArrayList; import java.util.Collections; public class GFG { public static void main(String a[]) throws Exception { // Create the Array List ArrayList<String> ArrList = new ArrayList<String>(); // add the values in Array List ArrList.add("Item 1"); ArrList.add("Item 2"); ArrList.add("Item 3"); ArrList.add("Item 4"); ArrList.add("Item 5"); // display Array List before swap System.out.println("Before Swap the ArrayList "); System.out.println(ArrList); // Swapping the elements at -1 and 2 indices // Throws IndexOutOfBounds Exception Collections.swap(ArrList, -1, 2); } } Output Exception in thread "main" java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 5 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248) at java.base/java.util.Objects.checkIndex(Objects.java:372) at java.base/java.util.ArrayList.get(ArrayList.java:458) at java.base/java.util.Collections.swap(Collections.java:501) at GFG.main(GFG.java:27) Comment More infoAdvertise with us Next Article How to Swap Two Elements in an ArrayList in Java? M mukulsomukesh Follow Improve Article Tags : Java Java Programs Java-ArrayList Practice Tags : Java Similar Reads 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 Swap Two Elements in a LinkedList in Java? Given a Linked List, the task is to swap two elements without disturbing their links. There are multiple ways to swap. Elements can be swapped using by swapping the elements inside the nodes, and by swapping the complete nodes. Example: Input :- 10->11->12->13->14->15 element1 = 11 el 4 min read How to Add an Element at Particular Index in Java ArrayList? 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. Ex 2 min read How to Shuffle the Elements of Array in Java? In Java, to shuffle the elements of an array, we can use the shuffle() method from the Collections class. This method is part of the java.util package. To shuffle an array, first, we need to convert the array into a List, shuffle it, and then convert it back to an array if needed. Example:Let's go t 3 min read How to Convert TreeMap to an ArrayList in Java? TreeMap is a part of the Java Collection framework. Java TreeMap contains values based on the key. It implements the NavigableMap interface and extends AbstractMap class. It provides an efficient means of storing key-value pairs in sorted order. Java TreeMap contains only unique elements. It cannot 4 min read Like