454,642 Members | 1,433 Online
Need help? Post your question and get tips & solutions from a community of 454,642 IT Pros & Developers. It's quick & easy.

# 2d array quick question

 P: 59 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? Expand|Select|Wrap|Line Numbers 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             }         } Sep 30 '13 #1
3 Replies

 Expert 2.5K+ P: 3,112 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 Expand|Select|Wrap|Line Numbers System.out.println(array[y][x]); rather than the current line 7 (or before it) and check whether the output is as expected. Sep 30 '13 #2

 P: 59 o ok. just want to make sure, is it loop though horizontally? Sep 30 '13 #3

 Expert 2.5K+ P: 3,112 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. Sep 30 '13 #4