Reputation: 43
public static void main(String[] args) {
int[][] array = {{1, 2, 3, 4}, {5, 6, 7, 8}};
System.out.println(m1(array)[0]);
System.out.println(m1(array)[1]);
}
public static int[] m1(int[][] m) {
int[] result = new int[2];
result[0] = m.length;
result[1] = m[0].length;
return result;
}
I am not able to understand how 'array' is passed in methods. According to code, we are trying to pass the 1st row of array in m1 as an argument while m1 method is having a 2-D array as its parameter.
System.out.println (m1 (array)[0]);
And in m1 hole array is passed how this program is working? and how I am able to pass (array)[0] in m1 without any parenthesis
Upvotes: 1
Views: 119
Reputation: 54148
The m1(array)[0]
doesn't pass the 1st row of array in m1 as an argument, that syntax means that it pass the whole array, then you take the first box of the result,
To understand it better, here is an equivalent code:
int[][] array = {{1, 2, 3, 4}, {5, 6, 7, 8}};
int[] res = m1(array);
System.out.println(res[0]); // will give the m.length, which is 2
System.out.println(res[1]); // will give the m[0].length, which is 4
You need to see it
m1(array) [0]
(method call, then first box of result), you could write (m1(array))[0]
if it helps to understandm1 (array)[0]
, because the parenthesis belong to the methodUpvotes: 3