Lets say we have a 8 x 8 2d integer array named grid and trying to select the element 0 at [5][5]
int[][] grid = new int{{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,0,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1}};
Now the question is, is it possible to access the index of element 0 without using coordinates and just use a single number?
example: access the element 0 with the number 45, like a 1d array
I tried approaching this problem with a for loop but it is giving me out of range errors.
int x = 0;
int y = 0;
for (int i = 0;i<45;i++) {
x += 1;
if (x > grid[y].length) {
x = 0;
y += 1;
}
The code above is supposed to add x and y until it reaches the target element.