Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a 2D array index into a 1D index

Tags:

java

arrays

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...

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

like image 877
Becky Avatar asked Nov 13 '09 18:11

Becky


People also ask

How do you represent a 2D array as a 1D array?

oneD_index = 3 * y + x; Where x is the position within the row and y the position in the column. Instead of 3 you use your column width. This way you can convert your 2D coordinates to a 1D coordinate.

Is 1D array faster than 2D?

Unless you are talking about static arrays, 1D is faster. Clearly the 2D case loses the cache locality and uses more memory. It also introduces an extra indirection (and thus an extra pointer to follow) but the first array has the overhead of calculating the indices so these even out more or less.

How is a 2D array indexed?

Two-dimensional (2D) arrays are indexed by two subscripts, one for the row and one for the column. Each element in the 2D array must by the same type, either a primitive type or object type.


5 Answers

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.

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.

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

like image 178
Michael Avatar answered Oct 04 '22 13:10

Michael


Given 4 columns by 8 rows then:

i = row * 4 + col

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

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

like image 40
Steve Wortham Avatar answered Oct 03 '22 13:10

Steve Wortham


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). 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.

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

like image 31
Matt Greer Avatar answered Oct 01 '22 13:10

Matt Greer


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

like image 43
mr grumpy Avatar answered Oct 02 '22 13:10

mr grumpy


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

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

like image 45
call-me Avatar answered Oct 01 '22 13:10

call-me