简体   繁体   English

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

[英]Convert a 2D array index into a 1D index

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]?

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).因此,假设您有一个大小为 5 x 3(5 行,3 列)的二维数组。 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.因此,每行有 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应该就是这样

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 + col would leave unnecessary gaps in the possible indexes. row * 8 + col会在可能的索引中留下不必要的间隙。

Every row in your 2D array is placed end to end in your 1D array.二维数组中的每一行都以首尾相连的方式放置在一维数组中。 i gives which row you are in, and j gives the column (how far into that row). i给出您所在的行, j给出列(该行有多远)。 so if you are in the ith row, you need to place i complete rows end to end, then append j more onto that to get your single array index.因此,如果您在第ith行,则需要将i完整的行首尾相连,然后在其上追加j以获得单个数组索引。

So it will be something like所以它会像
singleDimIndex = array[0].length * i + j

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

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输出:等于:真

If 2d array is 4 width .如果二维数组是 4 width 。 We have pos x and pos y of an element .我们有一个元素的 pos x 和 pos y 。

We need an index of it .我们需要它的索引。 How to do it ?怎么做 ?

Look at array it has 12 elements .看看它有 12 个元素的数组。

if you want to go to element nr 6 ,you go to element 2 in row 2 .如果要转到第 6 个元素,请转到第 2 行的第 2 个元素。

1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12

column is 1,2,3,4 row is: 1, 5, 9,列是 1,2,3,4 行是:1, 5, 9,

element 1 is (0,0) in 2d .元素 1 在 2d 中是 (0,0) 。

so use this = pos.y * width + pos.x ;所以使用 this = pos.y * width + pos.x ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM