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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Looping through a multidimensional array

  1. #1
    Member
    Join Date
    Jul 2009
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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);
    			}
    		}
    	}
    }
    Last edited by helloworld922; October 11th, 2009 at 05:34 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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. #3
    Member
    Join Date
    Jul 2009
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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!

Similar Threads

  1. Storing an array into an array
    By vluong in forum Collections and Generics
    Replies: 4
    Last Post: September 22nd, 2009, 02:14 PM
  2. [SOLVED] looping, for,while,do-while.
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: August 6th, 2009, 01:32 PM
  3. Replies: 24
    Last Post: April 14th, 2009, 03:43 PM