Arrays.asList()。包括()双VS双阵列 [英] Arrays.asList().contains() on Double vs double arrays
问题描述
我有一个双阵列 doubleArray1
。我尝试了 Arrays.asList()。包括()
如下图所示。
I have a double array doubleArray1
. I tried a Arrays.asList().contains()
operation as shown below
double doubleArray1 [] = {1D,2D,3D};
if(Arrays.asList(doubleArray1).contains(1D)) {
System.out.println("hello-1");
}
它不打印任何东西。然后,我做了它双阵列
It does not print anything. Then I made it a Double array
Double doubleArray1 [] = {1D,2D,3D};
if(Arrays.asList(doubleArray1).contains(1D)) {
System.out.println("hello-1");
}
它打印 HELLO-1
。
有人能解释为什么这种差异?
Could someone explain why this difference?
推荐答案
您第一次调用 Arrays.asList
实际上返回一个列表<双[]>
- 它的自动装箱的说法,因为双[]
不是 T []
...仿制药不允许原始类型的类型参数。
Your first call to Arrays.asList
is actually returning a List<double[]>
- it's autoboxing the argument, because a double[]
isn't a T[]
... generics don't allow for primitive types as type arguments.
如果你想转换一个双[]
到列表&LT;双&GT;
,你要么需要做的它手动或使用第三方库做。例如:
If you want to convert a double[]
into a List<Double>
, you either need to do it manually or use a third-party library to do it. For example:
public List<Double> toList(double[] doubles) {
List<Double> list = new ArrayList<>(doubles.length);
for (double x : doubles) {
list.add(x);
}
return list;
}
请注意,不像 Arrays.asList
随后到阵列的任何更改将的不的在列表反之亦然体现 - 这是一个副本,而不是视图。
Note that unlike Arrays.asList
any subsequent changes to the array will not be reflected in the list or vice versa - it's a copy, not a view.
这篇关于Arrays.asList()。包括()双VS双阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!