Random shuffling of an array
To shuffle an array randomly in Java, you can use the Collections.shuffle
method.
Here's an example of how you can use the Collections.shuffle
method to shuffle an array:
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
String[] array = {"a", "b", "c", "d", "e"};
List<String> list = Arrays.asList(array);
Collections.shuffle(list);
System.out.println(list);
}
}
This code defines an array of strings and converts it to a list using the Arrays.asList
method. It then shuffles the list using the Collections.shuffle
method and prints the shuffled list to the console.
The Collections.shuffle
method shuffles the elements of the list in place, so you don't need to create a new list or array to store the shuffled elements.
I hope this helps! Let me know if you have any questions.