2d array quick question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • game2d
    New Member
    • Apr 2013
    • 59

    2d array quick question

    how to loop though 2d-array horizontally from left to right.
    so when i loop though like 1,2,3,4,5,6,7,8

    i just want to make sure this loop is doing what i think it does?


    Code:
    int array[][] = {{1,2,3,4},
                     {5, 6, 7, 8}};
    
    
    for (int y= 0; y< array.length; y++) { 
        for (int x= 0; x < array[y].length; x++) { 
               array[y][x] == 2
    			}
    		}
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Apart from line 7 missing a semicolon at the end, it should be fine. To make sure it works you could however have just put a line like [code=java]System.out.prin tln(array[y][x]);[/code] rather than the current line 7 (or before it) and check whether the output is as expected.

    Comment

    • game2d
      New Member
      • Apr 2013
      • 59

      #3
      o ok. just want to make sure, is it loop though horizontally?

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        It will access the elements in the order 1, 2, 3, 4 and then 5, 6, 7, 8 if that's what you mean, yes.

        Comment

        Working...