简体   繁体   中英

Get specific element from array in Java

Basically I am trying to return an element from an 2d array in java. 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.

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.

You don't need the loop. Also, you need to convert the double to 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];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM