这个问题是针对 Java 的。我继续尝试创建一个大小为 N 的 Block[][],其中随机放置数字 0 - 9。我创建了一个数组,然后将其洗牌以防止数字重复。当我遍历数组并将数字分配给二维数组时,我不断发现二维数组中的所有数字都是相同的。该块不断出现:
222
222
222
非常感谢您的帮助!
public class Board {
int N = 3;
static int [][] copy;
//construct a board from an N-by-N array of blocks
//(where blocks[i][j] = block in row i, column j)
public Board(int[][] blocks){
blocks = new int[N][N]; //creates array of size N
//generates random numbers 0 inclusive to # exclusive
List<Integer> randomList = new ArrayList<>();
for (int i = 0; i < blocks.length; i++){
randomList.add(i);
}
int randomNum = 0;
for (int i = 0; i < blocks.length; i++){
randomNum = randomList.get(i);
}
Collections.shuffle(randomList);
for (int i = 0; i < blocks.length; i++){
for (int j = 0; j < blocks[i].length; j++){
blocks[i][j] = randomNum;
}
}
copy = blocks.clone();
}