Java Array Shuffle shuffle(Integer[] data)

Here you can find the source of shuffle(Integer[] data)

Description

shuffle

License

Creative Commons License

Declaration

public static void shuffle(Integer[] data) 

Method Source Code

//package com.java2s;
//<div>Icons made by <a href="http://www.flaticon.com/authors/freepik" title="Freepik">Freepik</a> from <a href="http://www.flaticon.com" title="Flaticon">www.flaticon.com</a> is licensed by <a href="http://creativecommons.org/licenses/by/3.0/" title="Creative Commons BY 3.0" target="_blank">CC 3.0 BY</a></div>

import java.util.Random;

public class Main {
    public static void shuffle(Integer[] data) {
        if (data == null)
            return;
        Random r = new Random();
        for (int i = 0; i < data.length; i++) {
            int n = r.nextInt(data.length);
            int m = r.nextInt(data.length);
            swap(data, n, m);//from   w  w w .  j  ava  2s . c  o  m
        }
    }

    public static <T extends Comparable<? super T>> void swap(T[] data, int a, int b) {
        T temp = data[a];
        data[a] = data[b];
        data[b] = temp;
    }
}

Related

  1. shuffle(int[] array, Random rnd)
  2. shuffle(int[] ary)
  3. shuffle(int[] input)
  4. shuffle(int[] list, Random rnd)
  5. Shuffle(int[] v)
  6. shuffle(List list)
  7. shuffle(List list)
  8. shuffle(List list, Random rnd)
  9. shuffle(List as)