我为什么不能在ArrayList的int类型? [英] Why I can't have int in the type of ArrayList?
问题描述
我要声明整数类型的ArrayList
为什么要在下面给我一个错误:
的ArrayList< INT> list1的=新的ArrayList<&诠释GT;();
但以下工作:
的ArrayList<整数GT; list1的=新的ArrayList<整数GT;();
的ArrayList
只能引用类型,而不是原语。 整数
是一个类,不是原始。
在声明的ArrayList<整数GT; list1的=新的ArrayList<整数GT;()
,你要创建一个的ArrayList
将存储整数
类型,而不是 INT
原始。
如果您想了解原始和引用类型之间的区别,请<一href=\"http://pages.cs.wisc.edu/~hasti/cs302/examples/primitiveVsRef.html\">http://pages.cs.wisc.edu/~hasti/cs302/examples/primitiveVsRef.html
I want to declare an ArrayList of type integer.
Why the following gives me an error:
ArrayList<int> list1 = new ArrayList<int>();
But the following works:
ArrayList<Integer> list1 = new ArrayList<Integer>();
ArrayList
can only reference types, not primitives. Integer
is a class, not a primitive.
When you declare ArrayList<Integer> list1 = new ArrayList<Integer>()
, you're creating an ArrayList
which will store the Integer
type, not the int
primitive.
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
这篇关于我为什么不能在ArrayList的int类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!