How to Shuffle the Elements of Array in Java? Last Updated : 25 Nov, 2024 Comments Improve Suggest changes Like Article Like Report In Java, to shuffle the elements of an array, we can use the shuffle() method from the Collections class. This method is part of the java.util package. To shuffle an array, first, we need to convert the array into a List, shuffle it, and then convert it back to an array if needed. Example:Let's go through the below example to shuffle an array using the basic shuffle() method of the Collection class. Java // Java Program to shuffle the // elements of an array import java.util.Arrays; import java.util.Collections; import java.util.List; public class ArrayShuffle { public static void main(String[] args) { Integer[] arr = {1, 2, 3, 4,}; // Convert array to list List<Integer> l = Arrays.asList(arr); // Shuffle the list Collections.shuffle(l); // Convert list back to array (if needed) arr = l.toArray(new Integer[0]); System.out.println(Arrays.toString(arr)); } } Output[4, 1, 3, 2] Note: shuffle( ) Method only works with Lists, you may need to convert primitive arrays like int[] to Integer[ ] or another wrapper class array before you can shuffle them.Table of ContentSyntax of shuffle() MethodOther Ways to Shuffle the Elements of an Array in JavaUsing Fisher-Yates Shuffle AlgorithmShuffle Array Containing CharactersSyntax of shuffle() Methodpublic static void shuffle(List<?> list)Parameter: list: A List<?> (a list of any type of elements). Return Type: This method does not return anything. Other Ways to Shuffle the Elements of an Array in JavaUsing Fisher-Yates Shuffle AlgorithmThe Fisher-Yates algorithm is an efficient way to shuffle an array in random order. It works by iterating over the array and swapping each element with a random element that comes after it (including itself). Java // Java Program to Shuffle the // elements of an array // using Fisher-Yates Shuffle Algorithm import java.util.*; public class FisherYatesAlgo { public static void main(String[] args) { Integer[] arr = {1, 2, 3, 4,}; // Fisher-Yates Shuffle Algorithm Random r = new Random(); for (int i = arr.length - 1; i > 0; i--) { // Random index from 0 to i int j = r.nextInt(i + 1); // Swap elements at i and j int t = arr[i]; arr[i] = arr[j]; arr[j] = t; } System.out.println("" + Arrays.toString(arr)); } } Output[1, 2, 3, 4] Shuffle Array Containing CharactersShuffling an array of characters is similar to shuffling any other type of array. We can use the Fisher-Yates algorithm or shuffle() methods mentioned earlier to shuffle an array of characters. Java // Java Program to Shuffle Array // Containing Characters import java.util.*; public class ShuffleCharacter { public static void main(String[] args) { char[] arr = {'A', 'B', 'C', 'D',}; // Fisher-Yates Shuffle for Characters Random r = new Random(); for (int i = arr.length - 1; i > 0; i--) { // Random index from 0 to i int j = r.nextInt(i + 1); // Swap characters at i and j char t = arr[i]; arr[i] = arr[j]; arr[j] = t; } System.out.println(" " + new String(arr)); } } Output CDAB Note: This example uses the same Fisher-Yates algorithm to shuffle the elements of a character array. Comment More infoAdvertise with us Next Article How to Shuffle the Elements of Array in Java? R rahul9yw89 Follow Improve Article Tags : Java Java Programs Java-Collections Java-Arrays Java Examples +1 More Practice Tags : JavaJava-Collections Similar Reads How to Swap Two Elements in an ArrayList in Java? We can swap two elements of Array List using Collections.swap() method. This method accepts three arguments. The first argument is the ArrayList and the other two arguments are the indices of the elements. This method returns nothing. Syntax: public static void swap(List list, int a, int b); Parame 2 min read How to Shuffle Characters in a String in Java? In this article, we will learn how to shuffle characters in a String by using Java programming. For this, we have taken a String value as input and the method is available in java.util package and this method takes a list as input. Steps to shuffle characters in a String in JavaFirst, take one Strin 2 min read Java Program to Shuffle Vector Elements Vectors basically fall in legacy classes but now it is fully compatible with collections. Java has many built-in functions to perform different operations on collections or other data types and one of them is shuffle. To shuffle Vector elements Collections.shuffle() method is used. It shuffle method 3 min read Java Program to Print the Elements of an Array An array is a data structure that stores a collection of like-typed variables in contiguous memory allocation. Once created, the size of an array in Java cannot be changed. It's important to note that arrays in Java function differently than they do in C/C++As you see, the array of size 9 holds elem 6 min read Java Program to Left Rotate the Elements of an Array In Java, left rotation of an array involves shifting its elements to the left by a given number of positions, with the first elements moving around to the end. There are different ways to left rotate the elements of an array in Java.Example: We can use a temporary array to rotate the array left by " 5 min read How to Declare an ArrayList with Values in Java? ArrayList is simply known as a resizable array. Declaring an ArrayList with values is a basic task that involves the initialization of a list with specific elements. It is said to be dynamic as the size of it can be changed. Proceeding with the declaration of ArrayList, we have to be aware of the co 2 min read Java Program to Sort the Elements of an Array in Ascending Order Here, we will sort the array in ascending order to arrange elements from smallest to largest, i.e., ascending order. So the easy solution is that we can use the Array.sort method. We can also sort the array using Bubble sort.1. Using Arrays.sort() MethodIn this example, we will use the Arrays.sort() 2 min read Java Program to Cyclically Permute the Elements of an Array Given an array of integers, there we cyclically permute its elements, that is, shift each array element to the left by one index. The first value will go into the last index. Example: Input: [1,2,3,4,5] Output: [2,3,4,5,1] Input: [2,3,1,5,6] Output: [3,1,5,6,2] Approach #1 In function cyclicShift(), 4 min read Java Program to Sort the Elements of an Array in Descending Order Here, we will sort the array in descending order to arrange elements from largest to smallest. The simple solution is to use Collections.reverseOrder() method. Another way is sorting in ascending order and reversing.1. Using Collections.reverseOrder()In this example, we will use Collections.reverseO 2 min read Shuffling Elements of Unordered Collections in Java Unordered Collections in java does not provide any order i.e. the elements cannot be accessed using specific indexing or ordering as we could in the case of ordered collections such as List. Sets and Maps are examples of unordered collections. In java, we cannot shuffle unordered collections directl 3 min read Like