Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Looping through a multidimensional array

1. ## Looping through a multidimensional array

Well I made myself a nice MultiDimensional array:
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;
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);
}
}
}
}

2. ## Re: Looping through a multidimensional array

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

3. ## The Following User Says Thank You to helloworld922 For This Useful Post:

MysticDeath (October 11th, 2009)

4. ## 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!