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 241 views
3 answers 344 views
1 answer 253 views
1 answer 248 views
1 answer 355 views
355 views asked Mar 15, 2021 by avibootz
1 answer 316 views
...