为什么基本数据类型没有在java.util.ArrayList中allowd? [英] Why primitive datatypes are not allowd in java.util.ArrayList?

查看:164
本文介绍了为什么基本数据类型没有在java.util.ArrayList中allowd?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/2504959/storing-primitive-values-in-a-java-collection\">Storing一个Java集合中的原始值?

ArrayList中只接受引用类型为元素,而不是原始数据类型。
当试图这样做,它会产生一个编译时错误。

ArrayList accepts only reference types as its element, not primitive datatypes. When trying to do so it produces a compile time error.

这背后的概念?这似乎是一个限制,不是吗?

What is the concept behind this? It seems like a limitation, is it not?

推荐答案

他们收集的对象的Java软件商店的内存位置的所有集合类。原始值不的适应的相同的定义。结果
为了解决这个问题,JDK5及以后有自动装箱 - 其中原语转换成合适的对象和回来时,他们是从集合添加或阅读

All collection classes of java store memory location of the objects they collect. The primitive values do not fit in to the same definition.
To circumvent this problem, JDK5 and onwards have autoboxing - wherein the primitives are converted to appropriate objects and back when they are added or read from the collections.

这篇关于为什么基本数据类型没有在java.util.ArrayList中allowd?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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