Here you can find the source of shuffle(T[] array)
Parameter | Description |
---|---|
array | The array that will be shuffled. |
public static <T> T[] shuffle(T[] array)
//package com.java2s; //License from project: Open Source License import java.util.concurrent.ThreadLocalRandom; public class Main { /**//w w w . j av a2 s . c o m * An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an {@code T} array. * * @param array The array that will be shuffled. * @return The shuffled array. */ public static <T> T[] shuffle(T[] array) { for (int i = array.length - 1; i > 0; i--) { int index = ThreadLocalRandom.current().nextInt(i + 1); T a = array[index]; array[index] = array[i]; array[i] = a; } return array; } /** * An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an {@code int} array. * * @param array The array that will be shuffled. * @return The shuffled array. */ public static int[] shuffle(int[] array) { for (int i = array.length - 1; i > 0; i--) { int index = ThreadLocalRandom.current().nextInt(i + 1); int a = array[index]; array[index] = array[i]; array[i] = a; } return array; } /** * An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an {@code long} array. * * @param array The array that will be shuffled. * @return The shuffled array. */ public static long[] shuffle(long[] array) { for (int i = array.length - 1; i > 0; i--) { int index = ThreadLocalRandom.current().nextInt(i + 1); long a = array[index]; array[index] = array[i]; array[i] = a; } return array; } /** * An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an {@code double} array. * * @param array The array that will be shuffled. * @return The shuffled array. */ public static double[] shuffle(double[] array) { for (int i = array.length - 1; i > 0; i--) { int index = ThreadLocalRandom.current().nextInt(i + 1); double a = array[index]; array[index] = array[i]; array[i] = a; } return array; } /** * An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an {@code short} array. * * @param array The array that will be shuffled. * @return The shuffled array. */ public static short[] shuffle(short[] array) { for (int i = array.length - 1; i > 0; i--) { int index = ThreadLocalRandom.current().nextInt(i + 1); short a = array[index]; array[index] = array[i]; array[i] = a; } return array; } /** * An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an {@code byte} array. * * @param array The array that will be shuffled. * @return The shuffled array. */ public static byte[] shuffle(byte[] array) { for (int i = array.length - 1; i > 0; i--) { int index = ThreadLocalRandom.current().nextInt(i + 1); byte a = array[index]; array[index] = array[i]; array[i] = a; } return array; } /** * An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an {@code float} array. * * @param array The array that will be shuffled. * @return The shuffled array. */ public static float[] shuffle(float[] array) { for (int i = array.length - 1; i > 0; i--) { int index = ThreadLocalRandom.current().nextInt(i + 1); float a = array[index]; array[index] = array[i]; array[i] = a; } return array; } /** * An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an {@code boolean} array. * * @param array The array that will be shuffled. * @return The shuffled array. */ public static boolean[] shuffle(boolean[] array) { for (int i = array.length - 1; i > 0; i--) { int index = ThreadLocalRandom.current().nextInt(i + 1); boolean a = array[index]; array[index] = array[i]; array[i] = a; } return array; } /** * An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an {@code char} array. * * @param array The array that will be shuffled. */ public static char[] shuffle(char[] array) { for (int i = array.length - 1; i > 0; i--) { int index = ThreadLocalRandom.current().nextInt(i + 1); char a = array[index]; array[index] = array[i]; array[i] = a; } return array; } }