Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
478 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...