Java 用双精度初始化 ArrayList

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23536493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:42:27  来源:igfitidea点击:

Initialize ArrayList with doubles

java

提问by R_vg

I'm trying to initialize an ArrayListto use later in my code, but it seems like it doesn't accept doubles.

我正在尝试初始化一个 ArrayList 以便稍后在我的代码中使用,但它似乎不接受双打。

public ArrayList<double> list = new ArrayList<double>();

public ArrayList<double> list = new ArrayList<double>();

It gives an error under 'double', sayin "Syntax error on token "double", Dimensions expected after this token"

它在“double”下给出错误,例如“标记“double”上的语法错误,此标记后预期的尺寸”

采纳答案by AntonH

An ArrayListdoesn't take raw data types (ie, double). Use the wrapper class (ie, Double) instead :

AnArrayList不采用原始数据类型(即,double)。改用包装类(即Double):

public ArrayList<Double> list = new ArrayList<>();

Also, as of Java 7, no need to specify that it's for the Doubleclass, it will figure it out automatically, so you can just specify <>for the ArrayList.

此外,从 Java 7 开始,无需指定它是用于Double类,它会自动计算出来,因此您只需<>为 ArrayList指定即可。

回答by anirudh

You need to use the Wrapper classfor doublewhich is Double. Try

您需要使用Wrapper classfor doublewhich is Double。尝试

public ArrayList<Double> list = new ArrayList<Double>();

回答by kviiri

In Java, ArrayLists (and other generic classes) only accept object references as types, not primitive data types. There are wrapper classes that allow you to emulate using primitives, though: Boolean, Byte, Short, Character, Integer, Long, Floatand Double;

在 Java 中,ArrayLists(和其他泛型类)只接受对象引用作为类型,而不接受原始数据类型。不过,有一些包装类允许您使用原语进行模拟:Boolean, Byte, Short, Character, Integer, Long, Floatand Double;

public ArrayList<Double> list = new ArrayList<Double>(); 
//or "public ArrayList<Double> list = new ArrayList<>();" in Java 1.7 and beyond

Values inside are "autoboxed" and "autounboxed" so you can treat doublesas Doubleswithout problems, and vice versa. You may need to explicitly specify whether you want arguments to be treated as intor Integerwhen dealing with lists of integral types, though, to disambiguate between cases like remove(int index)and remove(Object o).

里面的值是“自动装箱”和“自动拆箱”,因此您可以将其doubles视为Doubles没有问题,反之亦然。您可能需要显式地指定是否要参数被视为intInteger整数类型的列表打交道时,虽然样病例之间的歧义remove(int index)remove(Object o)

回答by user3145373 ツ

public ArrayList<Double> doubleList = new ArrayList<>();

From Java 1.7, you don't need to write Double while initializing ArrayList, so it's your choice to write Double in new ArrayList<>(); or new ArrayList<Double>();or not.. otherwise it's not compulsory.

在 Java 中1.7,你不需要写 Double while initializing ArrayList,所以你可以选择是否写 Double in new ArrayList<>(); or new ArrayList<Double>();.. 否则它不是强制性的。

Also known as a dynamic array.

也称为dynamic array.

No need to pre-determine the number of elements up front, just add to the array as we need it

不需要预先预先确定元素的数量,只需根据需要添加到数组中