Multidimensional Arrays | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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 ^^

24th Jan 2019, 10:19 PM
Gino ^^
Gino ^^ - avatar
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"); } } }
25th Jan 2019, 11:44 AM
Nopal Opal
Nopal Opal - avatar
0
what do you mean with automate ?
24th Jan 2019, 11:25 PM
Nopal Opal
Nopal Opal - avatar
0
Your method is widely used. You could make a function if you are going to be doing it for many arrays.
25th Jan 2019, 12:09 AM
James
James - avatar
25th Jan 2019, 5:36 AM
James
James - avatar
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 ^^
25th Jan 2019, 9:09 AM
Gino ^^
Gino ^^ - avatar
0
Ohh ok thank you ^^
25th Jan 2019, 11:49 AM
Gino ^^
Gino ^^ - avatar