2013-10-24 67 views
0

我正在检索地址表GeoCoderGoogleMapApi并在将结果提交给Spinner对象之前,我需要从列表中删除所有空条目。从ArrayList中删除空条目

我尝试以下两种方法,但他们都不做任何区别:

locs.removeAll(Arrays.asList("", null)); 
locs.removeAll(Collections.singleton(null)); 

-

private List<Address> getAddress(double latitude, double longitude,int maxResults) { 
    Geocoder gCoder = new Geocoder(getApplicationContext(),Locale.getDefault()); 
    try { 
     locs = gCoder.getFromLocation(latitude, longitude, maxResults); 
     locs.removeAll(Arrays.asList("", null)); 
     //locs.removeAll(Collections.singleton(null)); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    return locs; 

} 
+1

我认为第二种方式当然应该起作用。你有没有单独的列表尝试它? –

回答

0

试试这个

List locs = new ArrayList(); 
    locs.add(""); 
    locs.removeAll(Arrays.asList("", null)); 
    System.out.println(locs.size()); 

输出

0 

locs.add(""); 
    locs.removeAll(Collections.singleton(null)); 
    System.out.println(locs.size()); 

输出

1 

是有区别的

0

通过名单只是重复使用此代码

for (int i = 0; i < locs.size(); i ++){ 
     if (locs.get(i) == null){ 
      locs.remove(i); 
      i --; 
     } 
    } 
0

for(int i=0;i<list.size();i++) { if(list.get(i)==null||list.get(i).equals(""))// in case of string only otherwise only one枝ck就足够了 { list.remove(i); i--; } `}