无法在 1d 和 2d 数组值之间转换
Unable to convert between 1d and 2d array values
我意识到这个问题,解决方案遍布 Whosebug like here 但是我仍然无法完成这项工作。
大多数示例都说我只需要将行乘以宽度并添加列,这意味着 4x4 正方形网格中的位置 (4, 3) 将变为 (3 * 4 + 4) 或 16。到目前为止一切顺利。
例子说要取回坐标,我应该用索引除以 x 的行数,然后得到索引 y 的模数。对于上面的例子,应该是...
int x = 16 / 4;
int y = 16 % 4;
但这适用于某些值,而不适用于其他值。在这种情况下,当我在转换为索引后取回坐标时,我得到 (4,0)。这是有道理的,因为 4 均匀地进入 16,所以我必须缺少一些基本的东西。
这是我为尝试解决这个问题而创建的一些测试 Java 代码。我应该提到我的索引从 1 开始,所以左上角的第一个方块是 1,1,最后一个方块是 4,4。
public class Test {
int size;
public Test(int size) {
this.size = size;
}
public int toIndex(int x, int y) {
return x * this.size + y;
}
public int[] toCoordinates(int index) {
int coordinates[] = new int[2];
coordinates[0] = index / this.size;
coordinates[1] = index % this.size;
return coordinates;
}
public static void main(String[] args) {
int testSize = 4;
Test test = new Test(testSize);
for (int i = 1; i <= testSize; i++) {
for (int j = 1; j <= testSize; j++) {
int index = test.toIndex(i, j);
int coordinates[] = test.toCoordinates(index);
System.out.println(index + " == " + coordinates[0] + "," + coordinates[1]);
}
}
}
}
当前代码的输出是
5 == 1,1
6 == 1,2
7 == 1,3
8 == 2,0
9 == 2,1
10 == 2,2
11 == 2,3
12 == 3,0
13 == 3,1
14 == 3,2
15 == 3,3
16 == 4,0
17 == 4,1
18 == 4,2
19 == 4,3
20 == 5,0
所有数组都从 0 开始,试试这个:
public static void main(String[] args) {
int testSize = 4;
Test test = new Test(testSize);
for (int i = 0; i < testSize; i++) {
for (int j = 0; j < testSize; j++) {
int index = test.toIndex(i, j);
int coordinates[] = test.toCoordinates(index);
System.out.println(index + " == " + coordinates[0] + "," + coordinates[1]);
}
}
}
输出:
0 == 0,0
1 == 0,1
2 == 0,2
3 == 0,3
4 == 1,0
5 == 1,1
6 == 1,2
7 == 1,3
8 == 2,0
9 == 2,1
10 == 2,2
11 == 2,3
12 == 3,0
13 == 3,1
14 == 3,2
15 == 3,3 (16th)
看起来你在错误情况下交换了 x 和 y。位置应该是19,不是16.
Position [4,3] in a 4x4 array is (4*4)+3 = 19
floor(19 / 4) = 4
19 % 4 = 3
为了学习数组,我强烈建议坚持使用零索引数组,因为它们更直观地与内容实际存储在内存中的方式相关联。
我意识到这个问题,解决方案遍布 Whosebug like here 但是我仍然无法完成这项工作。
大多数示例都说我只需要将行乘以宽度并添加列,这意味着 4x4 正方形网格中的位置 (4, 3) 将变为 (3 * 4 + 4) 或 16。到目前为止一切顺利。
例子说要取回坐标,我应该用索引除以 x 的行数,然后得到索引 y 的模数。对于上面的例子,应该是...
int x = 16 / 4;
int y = 16 % 4;
但这适用于某些值,而不适用于其他值。在这种情况下,当我在转换为索引后取回坐标时,我得到 (4,0)。这是有道理的,因为 4 均匀地进入 16,所以我必须缺少一些基本的东西。
这是我为尝试解决这个问题而创建的一些测试 Java 代码。我应该提到我的索引从 1 开始,所以左上角的第一个方块是 1,1,最后一个方块是 4,4。
public class Test {
int size;
public Test(int size) {
this.size = size;
}
public int toIndex(int x, int y) {
return x * this.size + y;
}
public int[] toCoordinates(int index) {
int coordinates[] = new int[2];
coordinates[0] = index / this.size;
coordinates[1] = index % this.size;
return coordinates;
}
public static void main(String[] args) {
int testSize = 4;
Test test = new Test(testSize);
for (int i = 1; i <= testSize; i++) {
for (int j = 1; j <= testSize; j++) {
int index = test.toIndex(i, j);
int coordinates[] = test.toCoordinates(index);
System.out.println(index + " == " + coordinates[0] + "," + coordinates[1]);
}
}
}
}
当前代码的输出是
5 == 1,1
6 == 1,2
7 == 1,3
8 == 2,0
9 == 2,1
10 == 2,2
11 == 2,3
12 == 3,0
13 == 3,1
14 == 3,2
15 == 3,3
16 == 4,0
17 == 4,1
18 == 4,2
19 == 4,3
20 == 5,0
所有数组都从 0 开始,试试这个:
public static void main(String[] args) {
int testSize = 4;
Test test = new Test(testSize);
for (int i = 0; i < testSize; i++) {
for (int j = 0; j < testSize; j++) {
int index = test.toIndex(i, j);
int coordinates[] = test.toCoordinates(index);
System.out.println(index + " == " + coordinates[0] + "," + coordinates[1]);
}
}
}
输出:
0 == 0,0
1 == 0,1
2 == 0,2
3 == 0,3
4 == 1,0
5 == 1,1
6 == 1,2
7 == 1,3
8 == 2,0
9 == 2,1
10 == 2,2
11 == 2,3
12 == 3,0
13 == 3,1
14 == 3,2
15 == 3,3 (16th)
看起来你在错误情况下交换了 x 和 y。位置应该是19,不是16.
Position [4,3] in a 4x4 array is (4*4)+3 = 19
floor(19 / 4) = 4
19 % 4 = 3
为了学习数组,我强烈建议坚持使用零索引数组,因为它们更直观地与内容实际存储在内存中的方式相关联。