在我的Java教科书问题:Java:如何用不同的方法获取数组的长度?
完成这个类:
public class MyMatrix
{
public MyMatrix(int[][] elements)
{
// create new matrix with elements as the content
}
public int getRows()
{
// get the number of rows of the matrix
}
}
不要写方法体之外的任何代码。
这就是我想要做的事:
public class MyMatrix
{
public MyMatrix(int[][] elements)
{
int[][] matrix = elements;
}
public int getRows()
{
return matrix.length;
}
}
这显然是行不通的,因为matrix
是本地的构造。
我不明白我怎样才能使matrix
可用于其他方法,而无需在方法体外工作。有人可以告诉我如何做到这一点?
将矩阵存储为实例变量。 – Stultuske
您必须为您的班级添加一个int [] []'成员。也许这并不算作“方法体外的代码”。 – Eran