[英]2d array rows and cols Backwards ? -Java
Considering this 考虑到这一点
private int[][] goal = {{1,5,9,13}, {2,6,10,14}, {3,7,11,15},{4,8,12,0}};
public FifteenPuzzle1 (int[][] initialGrid) {
if (initialGrid == null)
throw new NullPointerException("null grid");
if (initialGrid.length != size)
throw new IllegalArgumentException("Grid does not have " + size + " rows");
for (int i = 0; i < size; i++) {
if (initialGrid[i] == null)
throw new NullPointerException("null row");
else if (initialGrid[i].length != size)
throw new IllegalArgumentException("Grid is not square");
}
grid = initialGrid;
boolean[] found = new boolean[size * size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (grid[i][j] < 0)
throw new IllegalArgumentException("Negative square found: " + grid[i][j]);
else if (grid[i][j] > size * size - 1)
throw new IllegalArgumentException("Overflow square found: " + grid[i][j]);
else if (found[grid[i][j]])
throw new IllegalArgumentException("Grid has multiple " + grid[i][j] + "s");
else {
found[grid[i][j]] = true;
if (grid[i][j] == 0) {
xspace = i; yspace = j;
}
}
}
}
sc = new SimpleCanvas("Fifteen Puzzle", gridsize, gridsize, bgColor);
sc.addMouseListener(this);
drawGrid();
}
when passing goal to the constructor i would expect the top row to read 1,5,9,13 not 1,2,3,4 which it currently does. 当将目标传递给构造函数时,我希望最上面一行读取的是1,5,9,13而不是当前所做的1,2,3,4。 what am i missing?
我想念什么?
When doing this: 执行此操作时:
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
...
}
}
The array is populated along the j
axis first, so you get 1,2,3,4 because that are the values at position 0 of the input. 该数组首先沿
j
轴填充,因此得到1,2,3,4,因为它们是输入位置0处的值。 To get it the way you want, either assign grid[j][i]
instead of [i][j]
要以您想要的方式获得它,请分配
grid[j][i]
而不是[i][j]
or 要么
turn around the input matrix 转过输入矩阵
or 要么
change the outer loops 改变外循环
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.