2013-08-04 32 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。 –