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 randomize a list in Java

1 Answer

0 votes
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class RandomizeAList_Java {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i <= 11; i++) {
            list.add(i);
        }

        Collections.shuffle(list, new Random());

        System.out.println(list.toString().replaceAll("[\\[\\],]", ""));
    }
}


    
/*
run:
        
11 9 4 1 0 10 8 7 5 3 6 2
 
*/

 



answered Aug 6, 2024 by avibootz

Related questions

2 answers 151 views
151 views asked Mar 28, 2021 by avibootz
2 answers 126 views
3 answers 192 views
1 answer 127 views
127 views asked Jun 30, 2023 by avibootz
1 answer 92 views
92 views asked Jun 30, 2023 by avibootz
1 answer 102 views
2 answers 445 views
...