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 207 views
1 answer 173 views
1 answer 168 views
1 answer 171 views
1 answer 210 views
...