Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,971 questions

51,913 answers

573 users

How to shuffle an array randomly in Java

2 Answers

0 votes
import java.util.Collections;
import java.util.Arrays;
import java.util.List;

public class MyClass {
    public static void main(String args[]) {
        String[] array = {"a", "b", "c", "d", "e", "f", "g"};
        
        List<String> list = Arrays.asList(array);
        
        Collections.shuffle(list);
        
        System.out.println(list);
    }
}
     
     
     
     
/*
run:
     
[d, f, e, c, g, a, b]

*/

 



answered Nov 8, 2023 by avibootz
0 votes
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class MyClass {
    public static void main(String args[]) {
        String[] array = {"a", "b", "c", "d", "e", "f", "g"};
        
        ArrayList<String> al = new ArrayList<String>(Arrays.asList(array));
 
        Collections.shuffle(al);
         
        array = al.toArray(array);
        
        System.out.println(Arrays.toString(array));
    }
}
     
     
     
     
/*
run:
     
[e, c, b, d, g, a, f]

*/

 



answered Nov 8, 2023 by avibootz

Related questions

1 answer 152 views
152 views asked Feb 20, 2018 by avibootz
1 answer 147 views
147 views asked Feb 19, 2018 by avibootz
2 answers 150 views
1 answer 169 views
2 answers 139 views
139 views asked Oct 30, 2021 by avibootz
1 answer 163 views
2 answers 108 views
108 views asked Mar 25, 2023 by avibootz
...