What is ArrayUtils.shuffle in Java?
Overview
shuffle is a static method of the ArrayUtils class that generates a random permutation of the array. shuffle rearranges the array elements in random order.
Characteristics
-
This method is in-place in nature, meaning the original array is modified.
-
The
Fisher-Yatesalgorithm is used to shuffle the array in a random manner.
Syntax
public static void shuffle(final int[] array)
Parameters
final int[] array- holds the array to be shuffled.
Return value
The method doesn’t return anything, as the shuffling is done to the original array passed as an argument.
Overloaded Methods
public static void shuffle(final boolean[] array)public static void shuffle(final byte[] array)public static void shuffle(final char[] array)public static void shuffle(final double[] array)public static void shuffle(final float[] array)public static void shuffle(final long[] array)public static void shuffle(final Object[] array)public static void shuffle(final short[] array)
Add Apache Commons-Lang package
ArrayUtils is defined in the Apache Commons Lang package. Apache Commons Lang can be added to the Maven Project by adding the following dependency to the pom.xml file.
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
For other versions of the commons-lang package, refer to the Maven Repository.
Import ArrayUtils
You can import the ArrayUtils class as follows:
import org.apache.commons.lang3.ArrayUtils;
Code
import org.apache.commons.lang3.ArrayUtils;public class Main {private static void shuffleFloatArray(){System.out.println("Shuffling Float Array");float[] array = {1.3f, 143.23f, 34.5f, 23.3f};System.out.print("Original Array - ");for(float f: array){System.out.print(f + " ");}ArrayUtils.shuffle(array);System.out.print("\nModified Array after shuffling - ");for(float f: array){System.out.print(f + " ");}}private static void shuffleCharArray(){System.out.println("Shuffling Character Array");char[] array = {'a','b','c','d'};System.out.print("Original Array - ");for(char c: array){System.out.print(c + " ");}ArrayUtils.shuffle(array);System.out.print("\nModified Array after shuffling - ");for(char c: array){System.out.print(c + " ");}}private static void shuffleIntArray(){System.out.println("Shuffling Integer Array");int[] array = {1,2,3,4,5};System.out.print("Original Array - ");for(int i: array){System.out.print(i + " ");}ArrayUtils.shuffle(array);System.out.print("\nModified Array after shuffling - ");for(int i: array){System.out.print(i + " ");}}public static void main(String args[]) {shuffleIntArray();System.out.println("\n-----------\n");shuffleCharArray();System.out.println("\n-----------\n");shuffleFloatArray();}}
Output
Shuffling Integer Array
Original Array - 1 2 3 4 5
Modified Array after shuffling - 2 5 3 1 4
-----------
Shuffling Character Array
Original Array - a b c d
Modified Array after shuffling - c a d b
-----------
Shuffling Float Array
Original Array - 1.3 143.23 34.5 23.3
Modified Array after shuffling - 34.5 1.3 143.23 23.3