Java 创建双数组列表的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20039098/
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
issue creating a double array list
提问by user2736640
Is there any reason the fallowing code would give A compile error ?
闲置的代码是否有任何原因会导致编译错误?
Import java.util.*;
public class Storageclass
// class used to store the Student data
{
// creates the private array list needed.
private ArrayList<String> nameList = new ArrayList<String>();
private ArrayList<double> GPAList = new ArrayList<double>();
private ArrayList<double> passedList = new ArrayList<double>();
}
this is in a class access by a main file there is more in the class by it not part of this error. when I run this the two double arrayList give me this error.
这是在主文件的类访问中,该类中有更多内容不是此错误的一部分。当我运行这个时,两个双 arrayList 给了我这个错误。
Storageclass.java:8: error: unexpected type
private ArrayList<double> GPAList = new ArrayList<double>(1);
^
required: reference
found: double
I am not sure why or what that error means any help would be appreciated.
我不确定为什么或该错误意味着任何帮助将不胜感激。
~ Thanks for the help was a embarrassingly novice mistake I made, but hope full this can help some other person.
〜感谢您的帮助是我犯的一个令人尴尬的新手错误,但希望这可以帮助其他人。
回答by Sotirios Delimanolis
Primitive types cannot be used as generic type arguments. Use the wrapper type Double
(or whichever is appropriate).
原始类型不能用作泛型类型参数。使用包装器类型Double
(或任何合适的)。
回答by brettw
Use ArrayList<Double> instead of ArrayList<double>.
使用 ArrayList< Double> 而不是 ArrayList<double>。
回答by Paul Samsotha
cant be primitive type
不能是原始类型
private ArrayList<double>
use Double
使用双
private ArrayList<Double>
回答by Pshemo
Since all generic types <T>
are erasedat runtime to Object
every type you put in place of T
must also extend Object. So you can't set T
to be primitive type like double
but you can use its wrapper class Double
. Try this way:
由于所有泛型类型<T>
都在运行时被擦除,因此Object
您放置的每个类型也T
必须扩展 Object。所以你不能设置T
为原始类型,double
但你可以使用它的包装类Double
。试试这个方法:
private List<Double> passedList = new ArrayList<Double>();
or since Java7 little shorter version
或者因为 Java7 更短的版本
private List<Double> passedList = new ArrayList<>();
Also don't worry if you try to add
variable of double
type to such array since it will be autoboxedto Double
.
如果您尝试将类型add
变量设置double
为此类数组,也不要担心,因为它将被自动装箱为Double
.