Swap elements of ArrayList - Java Collection Framework

Java examples for Collection Framework:ArrayList

Description

Swap elements of ArrayList

Demo Code


import java.util.ArrayList;
import java.util.Collections;
 
public class Main {
 
  public static void main(String[] args) {
    ArrayList arrayList = new ArrayList();
   //w  w  w.j a  v a  2s.c o m
    arrayList.add("A");
    arrayList.add("B");
    arrayList.add("C");
    arrayList.add("D");
    arrayList.add("E");
   
    System.out.println("Before swaping, ArrayList contains : " + arrayList);
   
    Collections.swap(arrayList,0,4);
   
    System.out.println("After swaping, ArrayList contains : " + arrayList);
   
  }
}

Result


Related Tutorials