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/