swap elements in a list java code example
Example 1: swap items in arraylist java
public class ArrayListExample
{
public static void main(String[] args)
{
ArrayList<String> list = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "e", "f"));
System.out.println(list);
Collections.swap(list, 1, 2);
System.out.println(list);
}
}
Example 2: java switch two items in an array
String temp = arr[1];
arr[1] = arr[2];
arr[2] = temp;