为什么我不能将'int'作为ArrayList的类型? [英] Why can't I have 'int' as the type of an ArrayList?
问题描述
我想声明类型 int
的 ArrayList
。
为什么以下内容会给我一个错误:
Why does the following give me an error:
ArrayList<int> list1 = new ArrayList<int>();
但以下作品:
ArrayList<Integer> list1 = new ArrayList<Integer>();
?
推荐答案
ArrayList
只能引用类型,而不是基元。 Integer
是一个类,而不是一个原始类。
ArrayList
can only reference types, not primitives. Integer
is a class, not a primitive.
声明 ArrayList< Integer> list1 = new ArrayList< Integer>()
,你正在创建一个 ArrayList
,它将存储 Integer
类型,而不是 int
原语。
When you declare ArrayList<Integer> list1 = new ArrayList<Integer>()
, you're creating an ArrayList
which will store the Integer
type, not the int
primitive.
如果你想了解原语和原语之间的区别参考类型,请查看 http://pages.cs.wisc.edu/~hasti/cs302 /examples/primitiveVsRef.html
If you want to read about the difference between primitive and reference types, check out http://pages.cs.wisc.edu/~hasti/cs302/examples/primitiveVsRef.html
这篇关于为什么我不能将'int'作为ArrayList的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!