shuffle Array - Java Collection Framework

Java examples for Collection Framework:Array Shuffle

Description

shuffle Array

Demo Code


//package com.java2s;

import java.util.*;

public class Main {
    public static void shuffleArray(int[] ar) {
        Random rnd = new Random();
        for (int i = ar.length - 1; i > 0; i--) {
            int index = rnd.nextInt(i + 1);
            // Simple swap
            int a = ar[index];
            ar[index] = ar[i];// ww  w.j a  va2s . com
            ar[i] = a;
        }
    }
}

Related Tutorials