[英]Java ArrayList of Doubles
Is there a way to define an ArrayList
with the double
type? 有没有办法用double
类型定义ArrayList
? I tried both 我试过了两个
ArrayList list = new ArrayList<Double>(1.38, 2.56, 4.3);
and 和
ArrayList list = new ArrayList<double>(1.38, 2.56, 4.3);
The first code showed that the constructor ArrayList<Double>(double, double, double)
is undefined and the second code shows that dimensions are required after double
. 第一个代码显示构造函数ArrayList<Double>(double, double, double)
未定义,第二个代码显示double
之后需要维度。
Try this: 尝试这个:
List<Double> list = Arrays.asList(1.38, 2.56, 4.3);
which returns a fixed size list. 返回固定大小的列表。
If you need an expandable list, pass this result to the ArrayList
constructor: 如果需要可扩展列表,请将此结果传递给ArrayList
构造函数:
List<Double> list = new ArrayList<>(Arrays.asList(1.38, 2.56, 4.3));
ArrayList list = new ArrayList<Double>(1.38, 2.56, 4.3);
needs to be changed to: 需要改为:
List<Double> list = new ArrayList<Double>();
list.add(1.38);
list.add(2.56);
list.add(4.3);
尝试这个,
ArrayList<Double> numb= new ArrayList<Double>(Arrays.asList(1.38, 2.56, 4.3));
You are encountering a problem because you cannot construct the ArrayList
and populate it at the same time. 您遇到了问题,因为您无法构造ArrayList
并同时填充它。 You either need to create it and then manually populate it as such: 您需要创建它,然后手动填充它:
ArrayList list = new ArrayList<Double>();
list.add(1.38);
...
Or, alternatively if it is more convenient for you, you can populate the ArrayList
from a primitive array containing your values. 或者,如果对您来说更方便,可以从包含值的基本数组中填充ArrayList
。 For example: 例如:
Double[] array = {1.38, 2.56, 4.3};
ArrayList<Double> list = new ArrayList<Double>(Arrays.asList(array));
You can use Arrays.asList to get some list (not necessarily ArrayList) and then use addAll() to add it to an ArrayList: 您可以使用Arrays.asList获取一些列表(不一定是ArrayList),然后使用addAll()将其添加到ArrayList:
new ArrayList<Double>().addAll(Arrays.asList(1.38L, 2.56L, 4.3L));
If you're using Java6 (or higher) you can also use the ArrayList constructor that takes another list: 如果您正在使用Java6(或更高版本),您还可以使用另一个列表的ArrayList构造函数:
new ArrayList<Double>(Arrays.asList(1.38L, 2.56L, 4.3L));
Try this: 尝试这个:
List<Double> l1= new ArrayList<Double>();
l1.add(1.38);
l1.add(2.56);
l1.add(4.3);
1) " Unnecessarily complicated " is IMHO to create first an unmodifiable List before adding its elements to the ArrayList. 1) “ 不必要的复杂 ”是IMHO在将其元素添加到ArrayList之前首先创建一个不可修改的List。
2) The solution matches exact the question: " Is there a way to define an ArrayList with the double type? " 2)解决方案完全匹配问题:“ 有没有办法用double类型定义ArrayList? ”
double type: 双重类型:
double[] arr = new double[] {1.38, 2.56, 4.3};
ArrayList: 数组列表:
ArrayList<Double> list = DoubleStream.of( arr ).boxed().collect(
Collectors.toCollection( new Supplier<ArrayList<Double>>() {
public ArrayList<Double> get() {
return( new ArrayList<Double>() );
}
} ) );
…and this creates the same compact and fast compilation as its Java 1.8 short-form: ...这会创建与Java 1.8简短形式相同的紧凑和快速编译:
ArrayList<Double> list = DoubleStream.of( arr ).boxed().collect(
Collectors.toCollection( ArrayList::new ) );
double[] arr = new double[] {1.38, 2.56, 4.3};
ArrayList<Double> list = DoubleStream.of( arr ).boxed().collect(
Collectors.toCollection( new Supplier<ArrayList<Double>>() {
public ArrayList<Double> get() {
return( new ArrayList<Double>() );
}
} ) );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.