2016-08-03 89 views
-2

我有一個包含20個元素的列表,但它包含15位置的空值,如何從List刪除空值?列表大小與它的大小不匹配

+1

你可以發佈用於執行此操作的代碼嗎?然後我們可以就如何改變它提出建議。 –

+0

非常不清楚的問題。你問如何從列表中刪除空元素? – shmosel

回答

1

我使用solution從@Lithium幾天研究

tourists.removeAll(Collections.singleton(null));

此代碼從列表中刪除所有空值。

1

如果你不想讓「空」形象出現在列表上,只是這樣做:

for each item on a list 
    if item == null then continue 
    else 
     add an item to <List>Images 

如果你傳遞一個空物體圖像的列表中,它仍然佔據放在列表中,即使這個地方在技術上未初始化。

1

你不能說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

相關問題