java遍历List

 初始数据,例如DAO层返回的商品列表List,如下:

List list = goodMapper.getGoodsBySid(sid);

1、迭代器遍历,方便删除元素

Iterator iterator = list.iterator();
while (iterator.hasNext()) {
	Goods good = iterator.next();
	if(good.getGid==20){
		iterator.remove();
	}
}

2、增强for循环

for (Goods good : list) {
	if (good.getGid==20) {
		list.remove(good);
		break;
	}
}

3、普通for循环

for (int i = 0; i < list.size(); i++) {
	Goods good = list.get(i);
	if (good.getGid==20) {
		list.remove(i);
		break;
	}
}

 

你可能感兴趣的:(Java)