配列の順序を乱す
1300 ワード
/**
* , , 。
* @author wWX154783
*/
public class Test_wzs9
{
private Random random = new Random();
//
private static final int SIZE = 10;
//
private int[] positions = new int[SIZE];
public Test_wzs9()
{
for (int index = 0; index < SIZE; index++)
{
// ,
positions[index] = index;
}
//
dwn();
}
//
public void changePosition()
{
for (int index = 0; index < 10; index++)
{
// 0 index , index
exchange(index, random.nextInt(index + 1));
}
dwn();
}
//
private void exchange(int p1, int p2)
{
int temp = positions[p1];
positions[p1] = positions[p2];
positions[p2] = temp;
}
//
private void dwn()
{
for (int index = 0; index < SIZE; index++)
{
System.out.print(positions[index] + " ");
}
System.out.println();
}
public static void main(String[] args)
{
Test_wzs9 rs = new Test_wzs9();
rs.changePosition();
rs.changePosition();
rs.changePosition();
}
}