我有一个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