2013-08-04 33 views
-1

我有一個String數組說如何算填補列數在數組

    String str = new String [5]; 

        str[0] = "Europe"; 

        str[1]= "America"; 

我怎樣才能返回我的數組填充列的數目?

+0

遍歷數組並計數。 – Thilo

回答

2

如果您沒有爲空元素分配任何值,則可以計數null

int count=0; 
for(final String s : str) { 
    if(s != null) ++count; 
} 
1

您可以使用Apache common lang's isBlank來檢查相同。

if (StringUtils.isBlank(str[i]) { 
    ++count; 
    ... 
} 
+0

謝謝你的回答,可能是我對apache的大門 – JavaFan

+0

@ElhadiMamoun如果你要探索添加第三方庫,那麼我肯定會考慮Guava而不是Apache Commons。 –