[英]Get specific element from array in Java
Basically I am trying to return an element from an 2d array in java. 基本上我试图从java中的2d数组返回一个元素。 I have created a separate Matrix class and inside the class I want to write a get_element method which would take as input the coordinates of the element I want from the matrix and the matrix itself, however I am not sure how to do this.
我创建了一个单独的Matrix类,并在该类内部编写了一个get_element方法,该方法会将矩阵中所需的元素的坐标以及矩阵本身作为输入,但是我不确定如何执行此操作。
public static double get_element(Matrix A, double m , double n)
{
for(int i=0;i<A.rows;i++)
for(int j=0;j<A.cols;j++)
return A.data[m][n];
}
This is how my code look right now. 这就是我的代码现在的样子。 And I get an error that says lossy conversion between double and int.
我得到一个错误,提示double和int之间有损转换。
You don't need the loop. 您不需要循环。 Also, you need to convert the double to
int
另外,您需要将double转换为
int
return A.data[(int) m][(int) n];
Alternatively (better), you change the method signature: 另外(更好),您可以更改方法签名:
public static double get_element(Matrix A, int m , int n) {
return A.data[m][n];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.