Java 将二维数组索引转换为一维索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1730961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:59:27  来源:igfitidea点击:

Convert a 2D array index into a 1D index

javaarrays

提问by Becky

I have two arrays for a chess variant I am coding in java...I have a console version so far which represents the board as a 1D array (size is 32) but I am working on making a GUI for it and I want it to appear as a 4x8 grid, so I have a 2-dimensional array of JPanels...

我有两个用于国际象棋变体的数组,我正在用 java 编码......到目前为止,我有一个控制台版本,它将棋盘表示为一维数组(大小为 32),但我正在为它制作一个 GUI,我想要它显示为 4x8 网格,所以我有一个二维的 JPanels 数组...

Question is, is there any formula that can convert the array[i][j] index into array[i] given the fact its a 4x8 array?

问题是,鉴于它是一个 4x8 数组,是否有任何公式可以将数组 [i][j] 索引转换为数组 [i]?

采纳答案by Steve Wortham

Given 4 columns by 8 rows then:

给定 4 列 x 8 行,则:

i = row * 4 + col

EDIT: My bad, nobody caught me on this mistake apparently. But it should actually be row * 4 + col.

编辑:我的错,显然没有人发现我犯了这个错误。但实际上应该是row * 4 + col

row * 8 + colwould leave unnecessary gaps in the possible indexes.

row * 8 + col会在可能的索引中留下不必要的空白。

回答by mr grumpy

i*8+j(assuming 8 is the horizontal width)

i*8+j(假设8是水平宽度)

回答by Matt Greer

Every row in your 2D array is placed end to end in your 1D array. igives which row you are in, and jgives the column (how far into that row). so if you are in the ithrow, you need to place icomplete rows end to end, then append jmore onto that to get your single array index.

二维数组中的每一行都以首尾相连的方式放置在一维数组中。i给出您所在的行,并j给出列(该行有多远)。因此,如果您在该ith行中,则需要将i完整的行首尾相连,然后将j更多行附加到该行上以获得单个数组索引。

So it will be something like
singleDimIndex = array[0].length * i + j

所以它会像
singleDimIndex = array[0].length * i + j

回答by Michael

Think of it this way:

可以这样想:

You have one array that happens to be a 1 dimensional array, which really, is just a long concatenation of items of a two dimensional array.

您有一个恰好是一维数组的数组,它实际上只是二维数组项的长连接。

So, say you have a two dimensional array of size 5 x 3 (5 rows, 3 columns). And we want to make a one dimensional array. You need to decide whether you want to concatenate by rows, or by columns, for this example we'll say the concatenation is by rows. Therefore, each row is 3 columns long, so you need to think of your one-dimensional array as being defined in "steps" of 3. So, the lengthy of your one dimensional array will be 5 x 3 = 15, and now you need to find the access points.

因此,假设您有一个大小为 5 x 3(5 行,3 列)的二维数组。我们想要制作一个一维数组。您需要决定是按行连接,还是按列连接,在本例中,我们会说连接是按行。因此,每行有 3 列长,因此您需要将一维数组视为在 3 的“步骤”中定义的。因此,一维数组的长度将为 5 x 3 = 15,现在您需要找到接入点。

So, say you are accessing the 2nd row and the 2nd column of your two dimensional array, then that would wind up being 3 steps (first row) + the number of steps in the second row, or 3 + 2 = 5. Since we are zero-based indexing that is -1, so that would be at index 4.

因此,假设您正在访问二维数组的第 2 行和第 2 列,那么最终将是 3 步(第一行)+ 第二行的步数,即 3 + 2 = 5。由于我们是从零开始的索引,即 -1,因此索引为 4。

Now for the specific formulation:

现在为具体配方:

int oneDindex = (row * length_of_row) + column; // Indexes

So, as an example of the above you would wind up having

所以,作为上面的一个例子,你最终会得到

oneDindex = (1 * 3) + 1

And that should be it

应该就是这样

回答by call-me

You could use this ArrayConvertor class to convert 2D arrays in 1D arrays and back.

您可以使用此 ArrayConvertor 类将二维数组转换为一维数组并返回。

Beware: Converting a 2D array to a normal one does only work with a matrix.

注意:将二维数组转换为普通数组只适用于矩阵。

public class ArrayConvertor {
    static public int[] d2Tod1(int[][] array){

        int[] newArray = new int[array.length*array[0].length];

        for (int i = 0; i < array.length; ++i) 
        for (int j = 0; j < array[i].length; ++j) {
            newArray[i*array[0].length+j] = array[i][j];
        }

        return newArray;
    }

    static public int[][] d1Tod2(int[] array, int width){

        int[][] newArray = new int[array.length/width][width];

        for (int i = 0; i < array.length; ++i) {
           newArray[i/width][i%width] = array[i];
        }

        return newArray;
    }
}

And Some testing code:

和一些测试代码:

public class JavaMain{
    public static void main(String[] args) {
        int[][] arr2D_1 = new int[4][8];

        byte counter=0;
        for (int i = 0; i < 4; i++) 
        for (int j = 0; j < 8; j++) {
            arr2D_1[i][j] = counter++;
        }

        int[]arr1D = ArrayConvertor.d2Tod1(arr2D_1);
        int[][] arr2D_2 = ArrayConvertor.d1Tod2(arr1D, 8);

        boolean equal = true;
        for (int i = 0; i < arr2D_1.length; i++) 
        for (int j = 0; j < arr2D_1[0].length; j++){ 
            if(arr2D_1[i][j]!=arr2D_2[i][j]) equal=false;
        }

        System.out.println("Equal: "+equal);
    }
}

Output: Equal: true

输出:等于:真