java - 使用返回数组中的元素

标签 java arrays

get1() 方法返回一个数组。我确实得到了输出,但是 get1() 的值是 [D@addbf1

我的问题是有什么方法可以直接使用返回的数组获取inf1[0]inf1[1]中的值并在输出语句中显示?

我知道显示输出的其他方式。但是我想知道我是否可以直接检索返回数组的元素。

class vehicle{
   double [] inf1 = new double[2];
   void set(double d, double s) {
     inf1[0]=d;
     inf1[1]=s;
   }
   double[] get1() {
     return inf1;
   }
}

public class calc2 {
   public static void main(String args[]) {
     vehicle ob = new vehicle();
     ob.set(56.24, 75);
     System.out.println("The time taken to cover "+ob.get1());
   }
}

最佳答案

My question is there any way to directly use the returned array to get the values in inf1[0] and inf1[1] and show it in the output statement?

当然:

double[] result = obj.get1();
System.out.println("Result: " + result[0] + "," + result[1]);

或者:

System.out.println("Result: " + Arrays.toString(obj.get1());

[D@addbf1 只是在数组上调用toString() 的结果。如果您想获取数组中的值,通常只需使用数组索引表达式单独访问每个元素:array[index]

(不清楚您的问题是关于访问数组值,还是为了显示目的将数组转换为文本。)

关于java - 使用返回数组中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11400601/

相关文章:

arrays - 在数组中查找一个元素,其中每个元素重复奇数次(但不止一次)并且只有一个出现一次

Java:如何在不调用方法的情况下实现指针效果?

java - 有没有一种简单的方法可以使用 RestEasy 通过 JSON 传递对象集合?

java - 在 spring boot rest api 中将图像发送到角度客户端?

php - 使用PHP将2个数组插入mysql

javascript - that.push 和 that.pop 元素是 "that={}"对象文字吗?

python - 如何在 Python 中 gzip 字节数组?

c - 在 C 中修改字符串数组中的字符串

Java EE - IntelliJ - javax.persistence.Table - 无法解析表名称

java - 如何在listview android中仅获取更新的复选框状态