By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,642 Members | 1,433 Online
Bytes IT Community
+ Ask a Question
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
  1. int array[][] = {{1,2,3,4},
  2.                  {5, 6, 7, 8}};
  3.  
  4.  
  5. for (int y= 0; y< array.length; y++) { 
  6.     for (int x= 0; x < array[y].length; x++) { 
  7.            array[y][x] == 2
  8.             }
  9.         }
Sep 30 '13 #1
Share this Question
Share on Google+
3 Replies


Nepomuk
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
  1. 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

Nepomuk
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

Post your reply

Sign in to post your reply or Sign up for a free account.