Swap elements of ArrayList in Java
Description
The following code shows how to swap elements of ArrayList.
Example
//from w ww . j a v a 2s. c o m
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("A");
arrayList.add("B");
arrayList.add("C");
arrayList.add("D");
arrayList.add("java2s.com");
System.out.println(arrayList);
Collections.swap(arrayList, 0, 4);
System.out.println(arrayList);
}
}
The code above generates the following result.