创建双数组列表的问题 [英] issue creating a double array list
问题描述
空闲代码是否有任何原因会导致编译错误?
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>();
}
这是在主文件的类访问中,该类中有更多内容不是此错误的一部分.当我运行这个时,两个 double arrayList 给了我这个错误.
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.
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.
推荐答案
由于所有泛型类型
都是 在运行时擦除到 Object
代替 T
的每种类型也必须扩展目的.所以你不能将 T
设置为像 double
这样的原始类型,但你可以使用它的包装类 Double
.试试这个方法:
Since all generic types <T>
are erased at 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:
private List<Double> passedList = new ArrayList<Double>();
或者从 Java7 开始的更短的版本
or since Java7 little shorter version
private List<Double> passedList = new ArrayList<>();
如果您尝试将 double
类型的 add
变量添加到这样的数组中,也不要担心,因为它将是 自动装箱到Double
.
Also don't worry if you try to add
variable of double
type to such array since it will be autoboxed to Double
.
这篇关于创建双数组列表的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!