0
Multidimensional Arrays
Heyya, assuming I have this method: public void array() { int[][] ar = {{1,2,3,4},{5,6,7,8},{9,0,1,2}}; for(int x = 0; x<ar.length; x++) { for(int y = 0; y<4; y++) { System.out.println(ar[x][y]); } } } Is there a way to automate y? Thanks ^^
6 Answers
+ 1
ooh i see, the only thing you should do is edit your expression in your second for loop (nested loop) to arr[x].length
public class Program {
public static void main(String[] args) {
int[][] ar = {{1,2,3,4},{5,6,7,8,9},{9,0,1,2}};
for(int x = 0; x < ar.length; x++) {
for(int y = 0; y < ar[x].length; y++) {
System.out.print(ar[x][y]);
}
System.out.print("\n");
}
}
}
0
what do you mean with automate ?
0
Your method is widely used. You could make a function if you are going to be doing it for many arrays.
0
I made this for JavaScript.
https://code.sololearn.com/WUjc4VsDLsTM/?ref=app
0
By automate I mean if there is a way to get the length of each part of the array. Right now if I use ar.length it will be 3 because there are three arrays within the array. But how can I figure out the length of each of these arrays ^^
0
Ohh ok thank you ^^