简体   繁体   English

2D int数组洗牌

[英]2D int array shuffle

im new to this but im tring to do a hotel reservation program. 我对此并不陌生,但打算进行酒店预订程序。

So i have a 2D int array with ALL rooms, when i start the program i want them to be randomly shuffle in to an array called RoomNotInUse or RoomInUse (so evertime i start the program the rooms are randomly generated. 所以我有一个2D int数组,其中包含所有房间,当我启动程序时,我希望它们随机地改编为一个名为RoomNotInUse或RoomInUse的数组(因此,每次我启动程序时,房间都是随机生成的。

Would be awesome if anyone know a way around this :) 如果有人知道解决这个问题的方法,那将是很棒的:)

// ARRAYS
protected static int[][] rooms = {
{1,1}, {1,2}, {1,3}, {1,4}, {1,5}, 
{2,1}, {2,2}, {2,3}, {2,4}, {2,5}, 
{3,1}, {3,2}, {3,3}, {3,4}, {3,5}, 
{4,1}, {4,2}, {4,3}, {4,4}, {4,5}, 
{5,1}, {5,2}, {5,3}, {5,4}, {5,5} 

};
//Declare all hotel rooms 5x5, the first number is the floor and the sec is the room
private char[][] ROIU = {

};
//Rooms not in use
private char[][] RIU = {

};
//Rooms in use


public class roomShuffle {

}
//Shuffle all rooms in 2 diffrent arrays, ROIN and RIU

public class RoomNotInUse {

}
//Displayes all the rooms thats not in use  

public class RoomInUse {

}
//Displayes all rooms in use

} }

You can use Fisher–Yates algorithm modified for two-dimensional arrays: 您可以使用为二维数组修改的Fisher-Yates算法:

void shuffle(int[][] a) {
    Random random = new Random();

    for (int i = a.length - 1; i > 0; i--) {
        for (int j = a[i].length - 1; j > 0; j--) {
            int m = random.nextInt(i + 1);
            int n = random.nextInt(j + 1);

            int temp = a[i][j];
            a[i][j] = a[m][n];
            a[m][n] = temp;
        }
    }
}

Assign all array into list. 将所有数组分配到列表中。 Than use Collections.shuffle() . 比使用Collections.shuffle()更好。

    List<int[]> pair=new ArrayList<int[]>();
    pair.addAll(Arrays.asList(rooms));

    Collections.shuffle(pair);

You can use shuffle - 您可以使用随机播放 -

Collections.shuffle()

Here's a tutorial that describes with or without Collections. 这是描述有无集合的教程

A generic shuffle method in Java should me similar to this Java中的通用洗牌方法应与此类似

The important thing is that you have to swap an item with a random element that comes after in the collection ;) 重要的是,您必须将项目与集合中后面的随机元素交换;)

public void shuffle(Comparable [] a){
    for(int i=0;i,a.length;i++)
         swap(a,i,getRandom(i,a.length-1);
}

private int getRandom(int min, int max){
     Random rnd = new Random();
     return min + rnd.nextInt(max-min+1);
}

 private void swap(Comparable [] a, int i, int j){
      Comparable temp = a[i];
      a[i]=a[j];
      a[j]=temp;
 }

Otherwise you can use the Collection.shuffle method. 否则,您可以使用Collection.shuffle方法。

Scanner scanner = new Scanner(System.in);
        int n;
        int m;
        int selection;
        System.out.println("Enter number of team");
        n = scanner.nextInt();

        m = (int) Math.sqrt(n);

        int[][] matrix = new int[m][m];
        List<Integer> listMatrix = new ArrayList<Integer>();

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < m; j++) {
                matrix[i][j] = i * m + j + 1;
                System.out.print(matrix[i][j] + "\t");
            }
            System.out.println();
        }

        for (int i = 0; i < m * m; i++) {
            listMatrix.add(i + 1);
        }
        System.out.println();

        do {
            System.out.println("what line is the card?");
            selection = scanner.nextInt();
        } while (!(selection >= 1 && selection <= m));

        selection -= 1;

        int[] values = new int[m];

        for (int i = 0; i < m; i++) {
            values[i] = matrix[selection][i];
            // System.out.print(values[i] + "\t");
        }
        System.out.println();

        Collections.shuffle(listMatrix);

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < m; j++) {
                matrix[i][j] = listMatrix.get(j + i * m);
                System.out.print(matrix[i][j] + "\t");
            }
            System.out.println();
        }

        scanner.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM