# Looping through a multidimensional array

• October 11th, 2009, 05:09 PM
MysticDeath
Looping through a multidimensional array
Well I made myself a nice MultiDimensional array:
Code :

``` int[][] newboard = {{0,0,0,0,6,0,0,0,9}, {0,6,0,0,0,0,3,0,1}, {2,0,0,0,0,9,8,0,0}, {1,9,6,0,0,2,0,0,8}, {0,0,0,3,0,8,0,0,0}, {8,0,0,9,0,0,1,7,5}, {0,0,1,7,0,0,0,0,4}, {6,0,7,0,0,0,0,8,0}, {4,0,0,0,9,0,0,0,0}};```
And I need to somehow scan through that board going from left to right, starting at the right side in the next row , printing out the array location of o's in the multidimensional array. This isn't homework, here's what I tried to do, it only prints out the 0's in the first row.

So for the first 0 on the top left in the newboard[][], I need it to return
X = 0;
Y = 0;

Here's what I tried, I'm not quite sure how to get it to read more than the first row;
Code :

```public void emptycellsearch(){ int y = 0; int x = 0; for(x = 0;x<=8;x++){ for(y = 0;y<=8;y++){ if(x==0 && newboard[x][y]==0){ System.out.println("X value is "); System.out.println(x); System.out.println("Y value is "); System.out.println(y); } } } }```
• October 11th, 2009, 05:37 PM
helloworld922
Re: Looping through a multidimensional array
Quote:

if(x==0 && newboard[x][y]==0)
It will only print out things if x equals zero (first row). Incidentally, I think you meant to do this:
Code :

```for (int row = 0; row <= 8; row++) { for(int col = 0; col <= 8; col++) { if (newboard[row][col] == 0) { //.. print out stuff } } }```

Rows run left/right, so their indices denote how far up/down they are, and columns run up/down, so their indices denote how far left/right you are (kind of confusing, but that's life).
• October 11th, 2009, 05:41 PM
MysticDeath
Re: Looping through a multidimensional array
Oh thanks, that's what I was getting wrong..
Yes this was what I meant to do.
Thank you!