Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
2d数组Java上元素的返回索引_Java - Fatal编程技术网

2d数组Java上元素的返回索引

2d数组Java上元素的返回索引,java,Java,我有一个3 x 3阵列private int[][]板 我想将索引[I][j]作为表示元素位置的整数 比如说 [0][0] = 0 [1][1] = 4 -- (middle spot) [2][0] = 6 -- (last line, first item) 是否有一种简单的方法代替每个职位的手工操作? 感谢/和%操作可以帮助您 int getByPosition(int[][] arr, int pos) { return arr[pos / arr.length][pos %

我有一个3 x 3阵列
private int[][]板

我想将索引
[I][j]
作为表示元素位置的整数

比如说

[0][0] = 0
[1][1] = 4  -- (middle spot)
[2][0] = 6 -- (last line, first item)
是否有一种简单的方法代替每个职位的手工操作?
感谢
/
%
操作可以帮助您

int getByPosition(int[][] arr, int pos) {
    return arr[pos / arr.length][pos % arr.length];
}
更新:按索引获取位置:

int getPosByIndex(int[][] arr, int i, int j) {
    return arr.length * i + j;
}

/
%
操作可以帮助您

int getByPosition(int[][] arr, int pos) {
    return arr[pos / arr.length][pos % arr.length];
}
更新:按索引获取位置:

int getPosByIndex(int[][] arr, int i, int j) {
    return arr.length * i + j;
}

这就是如何将
pos
值转换为
i,j
对的方法。您还应该演示如何将
i,j
对转换为
pos
值。这就是如何将
pos
值转换为
i,j
对。您还应该演示如何将
i,j
对转换为
pos
值。