在插入Java中的原始数组的ArrayList之后获取原始数组 [英] Getting back primitive array after insertion into an ArrayList of primitive arrays in Java
问题描述
List<double[]> x = new ArrayList<double[]>();
x.add(new double[]={1,2,3,4,54,6});
元素1,2,3,4,54,6被添加到x
elements 1,2,3,4,54,6 are added to x
x.get(0) ---> returns 1
但这样做会增加数组的地址吗?为什么
But doing this, address of array is added? why
List<double[]> x = new ArrayList<double[]>();
double[] name=new double[5];
name[0]=1
name[1]=3;
name[2]=3;
.
.
.
.
x.add(name);
getting x.get(0) ---> returns @as12cd2 (address of the array)
推荐答案
这是因为 List
由 double []
而不是 double $ c组成$ c>。
因为 double []
是一个数组,你必须指定该数组的位置同样。像这样:
Because a double[]
is an array, you have to specify the position from that array as well. Like so:
x.get(0)[0]
如果你想能够只使用 x.get()
,你的 List
必须由 double
原语组成。然后你可以使用一个单独的方法将 double
的数组添加到 List
(我不知道那个内置):
If you want to be able to just use x.get()
, your List
must be made up of double
primitives. Then you can add the array of doubles
to the List
using a separate method (I don't know of one that is built in):
List<Double> addArray(List<Double> o, double[] a) {
for(Double d : a)
o.add(d);
return o;
}
希望这是有道理的。我喜欢使用短变量名来使逻辑更清晰。只要你知道变量是什么,你应该没问题。
Hopefully that makes sense. I like using short variable names to make the logic more clear. As long as you know what the variables are, you should be fine.
这篇关于在插入Java中的原始数组的ArrayList之后获取原始数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!