简体   繁体   English

如何生成具有非重复数字的2d数组表?

[英]How do I generate 2d array table with non-repeating numbers?

How should I generate a 4X4 2D array table with every element different? 我应该如何生成每个元素不同的4X4 2D阵列表?

Here's my code: 这是我的代码:

public class Game {
public static void main(String[] args) {
    int gameboard[][] = new int[4][4];
    for (int row=0; row < gameboard.length; row++) {
        for (int col=0; col < gameboard[row].length; col++) {
            gameboard[row][col] = ((int)(1+Math.random() * 16));
            System.out.printf("%-4d",gameboard[row][col]);
        }
        System.out.println();
        }
      }

}

You could use a Set first to get no duplicates, and then a List to access them easily 您可以先使用Set来获取重复项,然后使用List来轻松访问它们

Set<Integer> set = new LinkedHashSet<>();
int gameboard[][] = new int[4][4];

while(set.size() != 4*4){
    set.add((int)(1+Math.random() * 16));
}

List<Integer> list = new ArrayList<>(set);

for (int row=0; row < gameboard.length; row++) {
    for (int col=0; col < gameboard[row].length; col++) {
        gameboard[row][col] = list.get(row*gameboard.length + col);
        System.out.printf("%-4d",gameboard[row][col]);
    }
    System.out.println();
    }
  }

You just need to put a control after like: 你只需要把控件放在后面:

 int a == gameboard[row][col];

 for (int row=0; row < gameboard.length; row++) {
     for (int col=0; col < gameboard[row].length; col++) {
             gameboard[row][col] = ((int)(1+Math.random() * 16));
             if(gameboard[row][col] == a){
                 col = col - 1; 
             }
     }
 }

Solution 1 : 解决方案1:

public static void main(String[] s1) throws Exception {
    int gameboard[][] = new int[4][4];
    Set<Integer> mySet = new HashSet<>();

    for (int row = 0; row < gameboard.length; row++) {
        for (int col = 0; col < gameboard[row].length; col++) {
            int randNum = (int) (1 + Math.random() * 16);

            while (mySet.contains(randNum)) {
                randNum = (int) (1 + Math.random() * 16);
            }
            mySet.add(randNum);

            gameboard[row][col] = randNum;
            System.out.printf("%-4d", gameboard[row][col]);
        }
        System.out.println();
    }
}

Here in each iteration we check if the random number generated is present in the set . 在每次迭代中,我们检查生成的随机数是否存在于集合中 If it is present then we loop until we get a different random number that is not present in the set. 如果它存在,那么我们循环直到我们得到一个不存在于集合中的不同随机数。


Solution 2 : 解决方案2:

List<Integer> myList = IntStream.range(1, 17).boxed().collect(Collectors.toList());
Collections.shuffle(myList);

for (int row = 0; row < gameboard.length; row++) {
    for (int col = 0; col < gameboard[row].length; col++) {
        gameboard[row][col] = myList.get(row * gameboard.length + col);
        System.out.printf("%-4d", gameboard[row][col]);
    }
    System.out.println();
}

Here we generate a list of numbers and then shuffle it using Collections.shuffle() . 在这里,我们生成一个数字列表,然后使用Collections.shuffle()将其洗牌。 Now we iterate over the multi-dimensional array and assign the values of the list to the array. 现在我们迭代多维数组并将列表的值分配给数组。

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

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