Traverse multi dimensional loop using enhanced for and if logic statement | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Traverse multi dimensional loop using enhanced for and if logic statement

19th Apr 2018, 12:38 PM
Purnaneel Ray
Purnaneel Ray - avatar
4 Answers
+ 5
int[][] arr = {{24, 42, 68, 86, 97}, {12, 21, 45, 54, 79}}; for(int[] i: arr) { for(int j: i) { System.out.print(j + " "); } } // Is this what you wanted? You don't need if logic, I guess.
19th Apr 2018, 1:13 PM
Dev
Dev - avatar
0
i used if to get the mode of the array. the dual for loop is alright but what i wanted to know is if the enhanced for can assign an element to traverse multi dimension array. here u have used one for to assign runs and other for element fetching. i was thinking of a way where the elements will be traversed in multi dimension index
19th Apr 2018, 1:35 PM
Purnaneel Ray
Purnaneel Ray - avatar
0
yes but i am not dealing with that. i am just using enhanced for to access multiple dimensions of the array. for(int a: arr) should traverse all dimensions but it is not doing so. i need a for to traverse both dimensions and another to get elements
19th Apr 2018, 1:46 PM
Purnaneel Ray
Purnaneel Ray - avatar