본문 바로가기
백엔드기술/개발언어

Shuffle Array

by RevFactory 2009. 12. 9.

package shufflearray;

import java.util.Random;

/**
 *
 * @author hwang
 */
public class Main {


    public static void main(String[] args) {
        int[] arr = {10,20,30,50,60,20};
        Random r = new Random();
        int[] newArr;

        for(int i = 0 ; i < 10 ; ++i) {
            newArr = shuffleArray(r, arr);
            for(int n : newArr) {
                System.out.print(n);
                System.out.print(" ");
            }
            System.out.println("");
        }
    }

    public static int[] shuffleArray(Random r, int[] nArr) {
        int[] newArr = nArr;
        int temp;
        for(int i = nArr.length-1 ; i > 0 ; --i) {
            int j = r.nextInt(i);
            if(i == j)
                continue;
            temp = newArr[i];
            newArr[i] = newArr[j];
            newArr[j] = temp;
        }
        return newArr;
    }

}


 

'백엔드기술 > 개발언어' 카테고리의 다른 글

프로세스와 스레드  (0) 2009.12.09
Swing Demo  (0) 2009.12.09
Parser Demo  (0) 2009.12.09