java获取数组的元素,从Java中的数组获取特定元素

这篇博客讨论了如何在Java中从2D数组(矩阵)中正确地获取指定位置的元素。作者遇到了一个错误,即在尝试将double类型的坐标转换为int类型时出现了“lossy conversion between double and int”的警告。解决方案是直接通过类型转换或修改方法参数为int类型,从而避免使用循环来访问数组元素。
摘要由CSDN通过智能技术生成

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

for(int j=0;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];

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值