shuffle two dimensional array - Java Collection Framework

Java examples for Collection Framework:Array Dimension

Description

shuffle two dimensional array

Demo Code


//package com.java2s;

import java.util.Random;

public class Main {
    public static <T> T[][] shuffle(T[][] input) {
        int j;//from  w  w  w. ja v a  2  s .c  o  m
        Random seed = new Random(System.nanoTime());
        T[][] cloned = input.clone();
        for (int i = cloned.length - 1; i > 0; i--) {
            j = seed.nextInt(i);
            for (int k = 0; k < cloned[i].length; k++) {
                T c = cloned[i][k];
                cloned[i][k] = cloned[j][k];
                cloned[j][k] = c;
            }
        }
        return cloned;
    }
}

Related Tutorials