简体   繁体   English

请帮我理解这个问题

[英]Please help me to understand this problem

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.根据代码,我们试图将 m1 中的第一行数组作为参数传递,而 m1 方法将二维数组作为其参数。

System.out.println (m1 (array)[0]);

And in m1 hole array is passed how this program is working?并且在 m1 孔数组中传递了这个程序是如何工作的? and how I am able to pass (array)[0] in m1 without any parenthesis以及我如何能够在没有任何括号的情况下在 m1 中传递 (array)[0]

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 , m1(array)[0]没有将 m1 中的第一行数组作为参数传递,该语法意味着它传递了整个数组,然后你取结果的第一个框,

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你需要看到它

  • as: m1(array) [0] (method call, then first box of result), you could write (m1(array))[0] if it helps to understand如: m1(array) [0] (方法调用,然后是结果的第一个框),如果有助于理解,您可以编写(m1(array))[0]
  • not as: m1 (array)[0] , because the parenthesis belong to the method not as: m1 (array)[0] ,因为括号属于方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM