0

Can anyone explain this java code?

public class MyClass {   public static voidmain(String[] args) {     int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };     for (int i = 0; i < myNumbers.length; ++i) {       for(int j = 0; j < myNumbers[i].length; ++j) {        System.out.println(myNumbers[i][j]);       }     }   } } The ques is, what is myNumbers[i]? We dont even assign that the first array is myNumbers[i] right? How does the system know?

4th Feb 2019, 3:39 PM
GO!
GO! - avatar
4 Answers
+ 3
In my view, the best way to understand the bidimensional arrays is visualizing them by matrix: int[][] myNumbers = new int{{1,2,3,4}, {5,6,7}}; it can be represented in this way: | column n.1 | column n.2 | column n.3 | column n.4| ---------------------------------------------------------------------------- row n.0 | 1 | 2 | 3 | 4 | row n.1 | 5 | 6 | 7 | | Then, you can say that the number of the rows are two, but the number of the columns depends by how many elements fill them in. In your case, if you want to consider every single row, you will have to digit: myNumbers[0]; that is equals to [1,2,3,4] myNumbers[1]; that is equals to [5,6,7] When you type myNumbers.length, you are retrieving the numbers of the rows which compose your array. Your array has got two rows => myNumbers.length = 2 If you want to know how many elements are in every row, you have to specify which row you want to consider and then you have to invoke the length method; myNumbers[0].length; it will retrieve the numbers of elements in the row n.0: row n.0 = [1, 2, 3, 4] there are 4 elements => myNumbers[0].length = 4 in the same way, you can notice that: row n.1 = [5, 6, 7] there are 3 elements => myNumbers[1].length = 3 The answer to your last question is that the system knows your array because you have initialized it, then, in the loop, it is only iterating for every "i" rows and for every "j" columns stored in the current row. I hope I have been clear, beacuse I know that is not an easy topic
4th Feb 2019, 8:31 PM
FabrizioPatriarca
+ 2
You can also look here: https://www.sololearn.com/learn/Java/2149/?ref=app Your code outprint all elements of your 2d array. myNumbers.length --> full size of the array. In this case 2 because inside the array are two arrays. myNumbers [i].length --> the size of the two arrays --> 4 and 3 myNumbers [0].length = 4 myNumbers [1].length = 3 In the first loop i starts at 0 and have to be < 2. Inside there is a second for loop. j starts at 0, if i = 0, j < 4; if i = 1, j < 3 i = 0; j = 0 --> myNumbers [0][0] = 1 i = 0; j = 1 --> myNumbers [0][1] = 2 ... i = 1; j = 2 --> myNumbers [1][2] = 7 Hope this helps.
4th Feb 2019, 6:45 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
Thank you guys! Btw, @Denise Roßberg the website cannot be found... Even so pls don't worry, I understand the code already!
7th Feb 2019, 12:26 AM
GO!
GO! - avatar
+ 1
GO! Strange. Maybe the link works only for the sololearn app.
7th Feb 2019, 5:59 AM
Denise Roßberg
Denise Roßberg - avatar