public class Otherit {
public static int[] m1(int[][] m) {
int[] result = new int[2];
result[0] = m.length;
result[1] = m[0].length;
return result;
}
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]);
}
}
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;
}
public static void main(String[] args) {
int[][] array = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 } };
//调用过程就是把二维数组传递到函数m1中,得到result数组,然后取索引值的第0个和第1个。
System.out.println(m1(array)[0]);
System.out.println(m1(array)[1]);
}
想要看懂这种复杂语句需要知道java运算符的优先级,优先级高得先计算,优先级相同根据结合性计算。
()和[]都是最高的优先级,它们的结合性是从左到右,所以是先运算m1(array),把结果记为x,则再运算x[0]
所以m1(array)[0]的运算就是获取将二维数组array通过m1方法改写为一维后的数组,再访问这个数组的第0号元素