集册 Java实例教程 洗牌T型数组

洗牌T型数组

欢马劈雪     最近更新时间:2020-01-02 10:19:05

481
洗牌T型数组


//package com.nowjava;
/*
 from NowJava.com 
*/


public class Main {

    /**

     * Shuffle an array of type T

     *

     * @param <T> The type contained in the array

     * @param array The array to be shuffled

     */

    public static <T> void shuffle(T[] array) {

        for (int i = array.length; i > 1; i--) {

            T temp = array[i - 1];

            int randIx = (int) (Math.random() * i);

            array[i - 1] = array[randIx];

            array[randIx] = temp;

        }/*n o w  j a v a  . c o m 提供*/

    }

}