How to swap elements of ArrayList in Java

1 Answer

0 votes
package javaapplication1;

import java.util.ArrayList;
import java.util.Collections;

public class JavaApplication1 {

    public static void main(String[] args) {

        ArrayList arrList = new ArrayList();

        arrList.add("aaa");
        arrList.add("bbb");
        arrList.add("ccc");
        arrList.add("ddd");
        arrList.add("eee");
        arrList.add("fff");
        arrList.add("ggg");
   
        Collections.swap(arrList, 0, 6);
   
        System.out.println(arrList);
  }
}
  
/*
run:

[ggg, bbb, ccc, ddd, eee, fff, aaa]

*/

 



answered Sep 26, 2016 by avibootz

Related questions

1 answer 168 views
3 answers 207 views
1 answer 173 views
1 answer 171 views
1 answer 281 views
281 views asked Mar 15, 2021 by avibootz
1 answer 210 views
...