java - 请帮我理解这个问题
问题描述
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]
如果有助于理解,您可以编写 - 不是:
m1 (array)[0]
,因为括号属于方法
推荐阅读
- python - OpenCV2 测量图像中物体之间的距离
- lua - Redis Lua解码Json返回空表
- flutter - 圆形缺口矩形
- node.js - 如果用户使用 Passport OAuth2 (Swoop) 和 Node/Express 输入无效电子邮件,我该如何重定向?
- ruby-on-rails - 无法写入未知属性“user_id”
- bouncycastle - 如何通过使用 BouncyCastle 的 client-maven-plugin 构建原生映像
- android - Kotlin:CountDownTimer onTick 被 onClick 中断
- javascript - 从http api获取json数据并只显示一个值?
- continuous-integration - 如何在 YAML 中正确引用 JSON(带变量)
- c# - 使用替换循环正则表达式