繁体   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;
}

我无法理解“数组”是如何在方法中传递的。 根据代码,我们试图将 m1 中的第一行数组作为参数传递,而 m1 方法将二维数组作为其参数。

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

并且在 m1 孔数组中传递了这个程序是如何工作的? 以及我如何能够在没有任何括号的情况下在 m1 中传递 (array)[0]

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

为了更好地理解它,这是一个等效的代码:

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

你需要看到它

  • 如: m1(array) [0] (方法调用,然后是结果的第一个框),如果有助于理解,您可以编写(m1(array))[0]
  • not as: m1 (array)[0] ,因为括号属于方法

暂无
暂无

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

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