我有以下类型ArrayList<List<String>>
Array_AcidExp[[Statistics (pH), Upright, Recumbent, Total], [Upright, Normal, Recumbent, Normal, Total, Normal], [Clearance pH : Channel 7], [Minimum, 4.69, , 2.42], [Maximum, 7.88, , 7.51, , 7.88], [Mean, 6.33, , 6.41, , 6.37], [Median, 6.62, , 6.40, , 6.49]]
我试过以下没有运气:
for (int i = 0; i < Arr_AcidExp_pattern_table2d.size(); i++) {
Arr_AcidExp_pattern_table2d.removeAll(Collections.singleton(null));
Arr_AcidExp_pattern_table2d.get(i).removeAll(Collections.singleton(" "));
}
我该怎样做才能摆脱空元素?
答案 0 :(得分:1)
这将删除所有内部空值
for (List<String> internal : Array_AcidExp) {
if (internal != null) {
for (int i = 0; i < internal.size(); i++) {
if (internal.get(i) == null) {
internal.remove(i)
}
}
}
}
没有运行它......
答案 1 :(得分:1)
你也可以在java8中使用removeIf()
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("yo");
list.add(null);
list.add(" ");
System.out.println(list);
list.removeIf(new Predicate<String>() {
@Override
public boolean test(String t) {
// removes all the elements from the list, for which the
// following condition returns true
return t == null || t.equals(" ");
}
});
System.out.println(list);
}