为什么我不能将'int'作为ArrayList的类型? [英] Why can't I have 'int' as the type of an ArrayList?

查看:174
本文介绍了为什么我不能将'int'作为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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆