0
Traverse multi dimensional loop using enhanced for and if logic statement
4 Réponses
+ 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.
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
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