列表大小与它的大小不匹配
答
如果你不想让“空”形象出现在列表上,只是这样做:
for each item on a list
if item == null then continue
else
add an item to <List>Images
如果你传递一个空物体图像的列表中,它仍然占据放在列表中,即使这个地方在技术上未初始化。
答
你不能说null
值意味着对象不存在,即使null
不是java中的对象。
String s = null
表示未设置String s
的引用,但声明已完成,该对象具有字符串类型并且被称为s。
看到关于它的更详细,oracle doc: The Kinds of Types and Values
public static void main(String[] args)
{
ArrayList al = new ArrayList();
al.add(null);
al.add("not null");
System.out.println(al.size()); //output 2
//if you wanna know how many objects inside of list and isn't null
int count=0;
for(Object obj:al)
if(!(obj==null))
count++;
System.out.println(count); //output 1
System.out.println(al); //output [null, not null] ← null is exist.
}
所以在你的情况下,大小()的返回应该是20
你可以发布用于执行此操作的代码吗?然后我们可以就如何改变它提出建议。 –
非常不清楚的问题。你问如何从列表中删除空元素? – shmosel