首页 > 解决方案 > 请帮我理解这个问题

问题描述

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]

标签: javaarrays

解决方案


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]如果有助于理解,您可以编写
  • 不是: m1 (array)[0],因为括号属于方法

推荐阅读