为什么我不能在ArrayList的类型中使用int?

时间:2022-10-20 22:26:11

I want to declare an ArrayList of type integer.

我想声明一个integer类型的ArrayList。

Why the following gives me an error:

为什么以下给我一个错误:

ArrayList<int> list1 = new ArrayList<int>();

But the following works:

但以下工作:

ArrayList<Integer> list1 = new ArrayList<Integer>();

7 个解决方案

#1


34  

ArrayList can only reference types, not primitives. Integer is a class, not a primitive.

ArrayList只能引用类型,而不是基元。整数是一个类,而不是一个原始类。

When you declare ArrayList<Integer> list1 = new ArrayList<Integer>(), you're creating an ArrayList which will store the Integer type, not the int primitive.

当声明ArrayList list1 = new ArrayList ()时,您将创建一个ArrayList,它将存储Integer类型,而不是int原语。

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

如果您想了解原始类型和引用类型之间的区别,请查看http://pages.cs.wisc.edu/~hasti/cs302/examples/primitiveVsRef.html

#2


11  

Because int is a primitive type. Only reference types can be used as generic parameters.

因为int是基本类型。只有引用类型可以用作通用参数。

#3


4  

The short answer is that generics (like ArrayList<Integer>) do not accept primitive types (int), only objects (Integer).

简短的回答是泛型(如ArrayList )不接受基本类型(int),只接受对象(Integer)。

This is because classes like ArrayList are implemented as using Objects. Since every class inherits from Object, the compiler can just plug in other classes. But primitive types (like int) do not inherit from Object, for they are not classes. So, Sun/Oracle made the Integer class to help with this.

这是因为像ArrayList这样的类被实现为使用Objects。由于每个类都继承自Object,因此编译器只需插入其他类即可。但原始类型(如int)不从Object继承,因为它们不是类。因此,Sun / Oracle制作了Integer类来帮助解决这个问题。

So, in short: int is not an Object.

所以,简而言之:int不是Object。

#4


0  

int is a primitive data type but Integer is a class so an arrayList array can only take reference types as its parameter not primitive type

int是原始数据类型,但Integer是一个类,因此arrayList数组只能将引用类型作为其参数而不是基本类型

#5


0  

int is a primitive. It is not a Object.

int是一个原语。它不是一个对象。

Refer this link for further details.

有关详细信息,请参阅此链接。

#6


0  

All the answers above answer why but the root of this question is frequent auto boxing and unboxing of the primitive data types. This problem is already solved by IntBuffer or ChadBuffer or you name the primitive type it's already there in the nio folder. Next time if you want to use primitive ArrayList don't use List instead use IntBuffer

上面的所有答案都回答了原因,但这个问题的根源是频繁的自动装箱和原始数据类型的拆箱。 IntBuffer或ChadBuffer已经解决了这个问题,或者你在nio文件夹中命名它已经存在的基本类型。下次如果要使用原始ArrayList,请不要使用List而是使用IntBuffer

#7


0  

int is not an Object and hence if list type is int, implementations of the list cannot be done.

int不是Object,因此如果列表类型是int,则无法完成列表的实现。

#1


34  

ArrayList can only reference types, not primitives. Integer is a class, not a primitive.

ArrayList只能引用类型,而不是基元。整数是一个类,而不是一个原始类。

When you declare ArrayList<Integer> list1 = new ArrayList<Integer>(), you're creating an ArrayList which will store the Integer type, not the int primitive.

当声明ArrayList list1 = new ArrayList ()时,您将创建一个ArrayList,它将存储Integer类型,而不是int原语。

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

如果您想了解原始类型和引用类型之间的区别,请查看http://pages.cs.wisc.edu/~hasti/cs302/examples/primitiveVsRef.html

#2


11  

Because int is a primitive type. Only reference types can be used as generic parameters.

因为int是基本类型。只有引用类型可以用作通用参数。

#3


4  

The short answer is that generics (like ArrayList<Integer>) do not accept primitive types (int), only objects (Integer).

简短的回答是泛型(如ArrayList )不接受基本类型(int),只接受对象(Integer)。

This is because classes like ArrayList are implemented as using Objects. Since every class inherits from Object, the compiler can just plug in other classes. But primitive types (like int) do not inherit from Object, for they are not classes. So, Sun/Oracle made the Integer class to help with this.

这是因为像ArrayList这样的类被实现为使用Objects。由于每个类都继承自Object,因此编译器只需插入其他类即可。但原始类型(如int)不从Object继承,因为它们不是类。因此,Sun / Oracle制作了Integer类来帮助解决这个问题。

So, in short: int is not an Object.

所以,简而言之:int不是Object。

#4


0  

int is a primitive data type but Integer is a class so an arrayList array can only take reference types as its parameter not primitive type

int是原始数据类型,但Integer是一个类,因此arrayList数组只能将引用类型作为其参数而不是基本类型

#5


0  

int is a primitive. It is not a Object.

int是一个原语。它不是一个对象。

Refer this link for further details.

有关详细信息,请参阅此链接。

#6


0  

All the answers above answer why but the root of this question is frequent auto boxing and unboxing of the primitive data types. This problem is already solved by IntBuffer or ChadBuffer or you name the primitive type it's already there in the nio folder. Next time if you want to use primitive ArrayList don't use List instead use IntBuffer

上面的所有答案都回答了原因,但这个问题的根源是频繁的自动装箱和原始数据类型的拆箱。 IntBuffer或ChadBuffer已经解决了这个问题,或者你在nio文件夹中命名它已经存在的基本类型。下次如果要使用原始ArrayList,请不要使用List而是使用IntBuffer

#7


0  

int is not an Object and hence if list type is int, implementations of the list cannot be done.

int不是Object,因此如果列表类型是int,则无法完成列表的实现。