【发布时间】:2015-01-22 07:07:55
【问题描述】:
即使 ArrayList 具有泛型类型参数,我是否可以将 null 值添加到它?
例如。
ArrayList<Item> itemList = new ArrayList<Item>();
itemList.add(null);
如果有,会
itemsList.size();
返回 1 还是 0?
如果我可以将null 值添加到ArrayList,我可以只循环包含此类项目的索引吗?
for(Item i : itemList) {
//code here
}
或者 for each 循环是否也会遍历列表中的空值?
【问题讨论】:
-
是的,1,不,是的。
-
stackoverflow.com/questions/5600668/… 与此类似,使用 null 而不是 0