【发布时间】:2016-12-02 17:02:29
【问题描述】:
我有一个类型为 Long 的 ArrayList,但我正在尝试在其中插入 int 值。我 期待 int也应该存储在 ArrayList 中,因为 int 的大小比 long 小。
int i=1;
ArrayList<Long> obj=new ArrayList<Long>();
obj.add(i);//It is showing error that Storing int is not possible
以下是可能的
-
列表项
long l=0; int i=5; l=i;
请提出 ArrayList 不能存储低值类型的原因。
【问题讨论】:
-
因为自动装箱只能将
long装箱到Long- 它不能将int装箱到Long。