Online Java Compiler By
JavaTpoint.com
import java.util.*; public class CollectionsSwapExample3 { public static void main (String[] args) { //Create a list with items List
list = Arrays.asList(44,55,99,77,88,66,33,22); System.out.println("List before swapping: "+list); Scanner sc = new Scanner(System.in); System.out.print("Enter index i : "); int i = sc.nextInt(); System.out.print("Enter index j : "); int j = sc.nextInt(); Collections.swap(list, i, j); System.out.println("List after swapping: "+list); sc.close(); } }
Output