In this article, we will discuss how to swap elements of List or ArrayList using Collections class’s utility swap() method
1. Swapping 2 elements of Java ArrayList:
Method signature:
public static void swap(List<Object> list, int i, int j);
In below example,
- we will swap element at index position-2 with element at index position-6
- printing ArrayList contents before and after swap
- iterating ArrayList using enhanced for-loop or forEach
Throws: IndexOutOfBoundsException for index out of range i.e.; 0 > index-position >= al.size();
Swap2ElementsOfJavaArrayList.java
package in.bench.resources.java.collection;
import java.util.ArrayList;
import java.util.Collections;
public class Swap2ElementsOfJavaArrayList {
public static void main(String[] args) {
// creating ArrayList object of type String
ArrayList<String> al = new ArrayList<String>();
// adding elements to ArrayList object
al.add("Narayan Murthy");
al.add("Dinesh");
al.add("Nandan Nilekeni");
al.add("Ashok Arora");
al.add("Shibulal");
al.add("Kris Gopalakrishnan");
al.add("Raghavan");
System.out.println("Before Swap: Iterating"
+ " ArrayList values as per Insertion Order\n");
// Iterating using enhanced for-loop
for(String str : al){
System.out.println(str);
}
// swapping elements of ArrayList using Collections.sort(al);
// element-3 (index-2) with element-7 (index-6)
Collections.swap(al, 2, 6);
System.out.println("\n\nAfter Swap: "
+ "index position 2nd with 6th\n");
// Iterating using enhanced for-loop
for(String str : al){
System.out.println(str);
}
}
}
Output:
Before Swap: Iterating ArrayList values as per Insertion Order
Narayan Murthy
Dinesh
Nandan Nilekeni
Ashok Arora
Shibulal
Kris Gopalakrishnan
Raghavan
After Swap index position 2nd with 6th
Narayan Murthy
Dinesh
Raghavan
Ashok Arora
Shibulal
Kris Gopalakrishnan
Nandan Nilekeni
Note: index position starts with 0 (i.e.; zero-based index)
Related Articles:
- Creating ArrayList using nCopies method of Collections class
- How to get size or length of an ArrayList
- Adding element to ArrayList at specified index position
- Remove element from ArrayList at specified index position
- How to delete a element and delete all elements of an ArrayList
- How to get sublist from ArrayList
- How to check whether particular element is present in ArrayList
- Comparing two ArrayList objects using containsAll() method
- Adding one ArrayList to another ArrayList using addAll() method
- Replacing ArrayList element with new value using set() method
- How to get maximum element from ArrayList
- How to get minimum element from ArrayList
- Various ways to iterate through ArrayList
- How to reverse ArrayList contents
- Sorting ArrayList in descending order
- Sorting ArrayList using sort() method of List in Java 8
- Sorting list of objects on multiple fields using Comparator in Java
- Remove duplicate elements from ArrayList in Java
- How to remove duplicate elements of ArrayList maintaining insertion order
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/list.html
- https://docs.oracle.com/javase/7/docs/api/java/util/List.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html
- https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
- https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
Happy Coding !!
Happy Learning !!