从ArrayList中删除空条目

时间:2013-10-24 16:20:21

标签: java android list arraylist google-maps-android-api-2

我正在检索GeoCoder GoogleMapApi 的地址,在将结果传递给Spinner对象之前,我需要从列表中删除所有空条目。< / p>

我尝试了两种方法,但没有一种方法有任何区别:

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;

}

3 个答案:

答案 0 :(得分:0)

使用此代码重复列表

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

答案 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

存在差异

答案 2 :(得分: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检查就足够了 { list.remove(i); i--; } `}