Java 二维整数数组洗牌

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20190110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:51:54  来源:igfitidea点击:

2D int array shuffle

javaarraysmultidimensional-arrayshuffle

提问by Erazx

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

}

}

回答by Sajal Dutta

You can use shuffle-

您可以使用随机播放-

Collections.shuffle()

Here's a tutorialthat describes with or without Collections.

这是一个介绍有或没有集合的教程

回答by Alessio

A generic shuffle method in Java should me similar to this

Java 中的通用 shuffle 方法应该与此类似

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 方法。

回答by Masudul

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);

回答by bvitaliyg

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;
        }
    }
}

回答by Oguzhan Cevik

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();