public static void swap(List list, int i, int j)
Listfruits = new ArrayList<>(Arrays.asList("apple", "banana", "cherry")); // swapping the elements at index 0 and 2 Collections.swap(fruits, 0, 2); System.out.println(fruits); // Output: [cherry, banana, apple]
String[] colors = {"red", "green", "blue"}; // swapping the elements at index 1 and 2 Collections.swap(Arrays.asList(colors), 1, 2); System.out.println(Arrays.toString(colors)); // Output: [red, blue, green]This example swaps the elements at index 1 and 2 in the `colors` array by converting it to a list and then using the `swap` method. The `java.util.Collections` class is part of the Java Collections Framework library.