我有一个函数,它接受一个二维数组。我想知道是否有任何方法可以在不迭代它的情况下获取二维数组的行和列。方法签名不能更改。
这个函数是在 ninetyDegRotator
类中。
public static int [][] rotate(int [][] matrix){
int [][] rotatedMatrix = new int[4][4];//need actual row n col count here
return rotatedMatrix; //logic
}
主要代码如下:
public static void main(String args[]){
int [][] matrix = new int[][]{
{1,2,3,4},
{5,6,7,8},
{9,0,1,2},
{3,4,5,6}
};
System.out.println("length is " + matrix.length);
int [][] rotatedMatrix = ninetyDegRotator.rotate(matrix);
}
同时matrix.length
给了我4
。所以我猜它给出的是一维数组中包含数组的引用数量的行数。那么有没有一种方法可以在不迭代的情况下获得计数呢?
int [][] rotatedMatrix = new int[matrix.length][matrix[0].length];
谢谢! - Aniket Thakur