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

查看:1054
本文介绍了我为什么不能在ArrayList的int类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要声明整数类型的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屋!

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