从Java中的数组获取特定元素 [英] Get specific element from array in Java

查看:1454
本文介绍了从Java中的数组获取特定元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我试图从java中的2d数组返回一个元素.我已经创建了一个单独的Matrix类,并且在该类内部我想编写一个get_element方法,该方法将以矩阵和矩阵本身的形式输入想要的元素的坐标,但是我不确定如何做到这一点.

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];


}

这是我的代码现在的样子.而且我收到一个错误,提示double和int之间有损转换.

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

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];
}

这篇关于从Java中的数组获取特定元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆