How does random shuffling of an array

0 votes

I need to randomly shuffle the following Array:

int[] solArr = {1, 2, 3, 4, 5, 6, 6, 5, 4, 3, 2, 1};

Is there any function to do that?

Jun 27, 2018 in Java by samarth295
• 2,220 points
923 views

2 answers to this question.

0 votes

simplest code to shuffle:

import java.util.*;
public class ch {
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        ArrayList<Integer> l=new ArrayList<Integer>(10);
        for(int i=0;i<10;i++)
            l.add(sc.nextInt());
        Collections.shuffle(l);
        for(int j=0;j<10;j++)
            System.out.println(l.get(j));       
    }
}
answered Jun 27, 2018 by scarlett
• 1,290 points
0 votes

Here is a simple way using an ArrayList:

List<Integer> solution = new ArrayList<>();
for (int i = 1; i <= 6; i++) {
    solution.add(i);
}
Collections.shuffle(solution);
answered Nov 2, 2018 by Sushmita
• 6,920 points

Related Questions In Java

0 votes
0 answers

How to declare an array of different data types

In Java, I am working with arrays, ...READ MORE

Aug 3, 2022 in Java by krishna
• 2,820 points
2,250 views
0 votes
0 answers

How to convert an Object {} to an Array [] of key-value pairs in JavaScript

I'd want to transform the following object: {"1":5,"2":7,"3":0,"4" ...READ MORE

Sep 28, 2022 in Java by Nicholas
• 7,760 points
1,147 views
0 votes
0 answers

How to sort an array of integers correctly

Trying to retrieve the greatest and lowest ...READ MORE

Sep 28, 2022 in Java by Nicholas
• 7,760 points
715 views
0 votes
1 answer

How to filter an array from all elements of another array

This is what I would do: var arr1 = [1,2,3,4], ...READ MORE

answered Nov 7, 2022 in Java by Damonlang
• 700 points
4,218 views
+1 vote
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,620 points
1,343 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,220 points
3,617 views
0 votes
2 answers

What is the syntax to initialize an array?

Rather than learning un-Official websites learn from ...READ MORE

answered Aug 2, 2018 in Java by samarth295
• 2,220 points
1,042 views
0 votes
1 answer

How to print java array in the simplest way?

String[] arr = new String[] {"John", "Mary", ...READ MORE

answered Apr 17, 2018 in Java by sophia
• 1,400 points
848 views
+1 vote
1 answer

Remove objects from an array in Java?

We can use external libraries: org.apache.commons.lang.ArrayUtils.remove(java.lang.Object[] array, int ...READ MORE

answered Jun 26, 2018 in Java by scarlett
• 1,290 points
1,204 views
0 votes
1 answer

how to play sound in java?

 The following code will work fine. But I ...READ MORE

answered Jun 20, 2018 in Java by scarlett
• 1,290 points
1,503 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP