2016-04-25 53 views
0

我有它的類型是ArrayList<List<String>>如何擺脫來自內部的ArrayList null元素的二維數組列表

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

的ArrayList的類型? – Priyamal

+0

對不起。它下降了。現在編輯 –

+0

請看看Java代碼約定。它會使你的代碼對於其他java開發者來說更​​具可讀性。 一個很好的鏈接是:https://google.github.io/styleguide/javaguide.html – gba

回答

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) 
       } 
      } 
     } 
    } 

沒跑它...

+0

沒有錯誤,但沒有取代空值。嘗試與internal.get(i)==「」。 –

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); 
}