ArrayList嵌套HashMap怎么遍历

如题:
List list = new ArrayList();
List<NeighboringCellInfo> cellList = tm.getNeighboringCellInfo();
for (NeighboringCellInfo info : cellList) {
Map map = new HashMap();
map.put("cid", info.getCid());
map.put("lac", info.getLac());
list.add(map);
}
如何遍历ArrayList,如果要指定删除其中一条,如何删除?求详细代码,主要是如何遍历

遍历
for(Map map:list)
{
// map xxx
}
删除 list.remove(i)或者list.remove(map),不能边遍历边删除,如果需要实现删除符合指定内容的map,可以先把符合内容的map放大临时list tempList里面,然后再remove(tempList)

还有,为啥用这么坑的数据结构啊,map最多只有两个对象,有这个必要吗?NeighboringCellInfo这个对象不是完全能代替map么追问

嗯,小弟我是初学者,不怎么懂!请教一下,
List cellList = tm.getNeighboringCellInfo();

这个是手机获取附近基站的信息,其中包括CID和LAC两个数据,他可以获取到好几条这样的数据,我想就是如何遍历出来,其中CID可能为0,我想把其中CID为0的那条删除,这样该如何做?希望又详细代码,谢谢了

追答

不知道我理解得对不对
List list = new ArrayList();
List cellList = tm.getNeighboringCellInfo();
for (NeighboringCellInfo info : cellList) {
if(info.getCid()==0) //如果cid是封装类型最好判断一下是否为null
list.add(info);
}
cellList.removeAll(list);
cellList就是你要的
或者
List list = new ArrayList();
List cellList = tm.getNeighboringCellInfo();
for (NeighboringCellInfo info : cellList) {
if(info.getCid()!=0) //如果cid是封装类型最好判断一下是否为null
list.add(info);
}
list就是你要的

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-07-23
你是想在遍历的时候删除吗?
是就不能用ArrayList

可以用List list=Collections.synchronizedList(new CopyOnWriteArrayList())

遍历就和你的代码中的for是一样的
相似回答