【问题标题】:2D int array shuffle2D int 数组洗牌
【发布时间】:2013-11-25 10:43:13
【问题描述】:

我对此很陌生,但我想做一个酒店预订计划。

所以我有一个包含所有房间的 2D int 数组,当我启动程序时,我希望它们随机进入一个名为 RoomNotInUse 或 RoomInUse 的数组(所以每次我启动程序时,房间都是随机生成的。

如果有人知道解决这个问题的方法会很棒:)

// 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

}

【问题讨论】:

    标签: java arrays multidimensional-array shuffle


    【解决方案1】:

    您可以使用针对二维数组修改的 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;
            }
        }
    }
    

    【讨论】:

    • 完美。谢谢
    【解决方案2】:

    将所有数组分配到列表中。比使用Collections.shuffle()。

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

    【讨论】:

      【解决方案3】:

      你可以使用shuffle-

      Collections.shuffle()
      

      这是一个tutorial,描述了有或没有集合。

      【讨论】:

      • 我试过这个,但不能让它与 2d 一起工作。但是当我阅读 aoubt shuffle 时,它​​并没有说明必须是什么样的列表。
      • @Erazx 请参阅我在回答中提供的教程链接。这很简单。 ArrayList、LinkedList ..它们都实现了列表。所以选择你喜欢的。在教程中还有数组洗牌。如果您愿意,可以使用它。
      【解决方案4】:

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

      重要的是你必须用集合中的随机元素交换一个项目;)

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

      否则你可以使用 Collection.shuffle 方法。

      【讨论】:

        【解决方案5】:
        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();
        

        【讨论】:

          猜你喜欢
          • 2015-04-15
          • 2013-06-28
          • 1970-01-01
          • 1970-01-01
          • 2019-02-07
          • 1970-01-01
          • 1970-01-01
          • 2010-12-03
          相关资源
          最近更新 更多