我有一個String數組說如何算填補列數在數組
String str = new String [5];
str[0] = "Europe";
str[1]= "America";
我怎樣才能返回我的數組填充列的數目?
我有一個String數組說如何算填補列數在數組
String str = new String [5];
str[0] = "Europe";
str[1]= "America";
我怎樣才能返回我的數組填充列的數目?
如果您沒有爲空元素分配任何值,則可以計數null
。
int count=0;
for(final String s : str) {
if(s != null) ++count;
}
您可以使用Apache common lang's isBlank來檢查相同。
if (StringUtils.isBlank(str[i]) {
++count;
...
}
謝謝你的回答,可能是我對apache的大門 – JavaFan
@ElhadiMamoun如果你要探索添加第三方庫,那麼我肯定會考慮Guava而不是Apache Commons。 –
遍歷數組並計數。 – Thilo