How to shuffle ArrayList elements in Java

1 Answer

0 votes
import java.util.ArrayList;
import java.util.Collections;
 
public class MyClass {
    public static void main(String[] args) {
        ArrayList<String> al = new ArrayList<String>();
        
        al.add("java");
        al.add("c++");
        al.add("python");
        al.add("c#");
        al.add("c");
        
        System.out.println(al);

        Collections.shuffle(al);
        
        System.out.println(al);
    }
}
 
 
 
/*
run:
 
[java, c++, python, c#, c]
[c#, c, java, python, c++]
 
*/

 



answered Mar 15, 2021 by avibootz

Related questions

3 answers 344 views
1 answer 252 views
1 answer 240 views
1 answer 248 views
1 answer 316 views
...