一.問題:如果不加break會報ConcurrentModificationException異常
1.第一種方法加break
List<ShopcartBO> shopcartList = JsonUtils.jsonToList(shopcartJson, ShopcartBO.class);
// 判斷購物車中是否存在已有商品,如果有的話則刪除
for (ShopcartBO sc: shopcartList) {
String tmpSpecId = sc.getSpecId();
if (tmpSpecId.equals(itemSpecId)) {
shopcartList.remove(sc);
break;
}
}
2.第二種方法:iterator
List<ShopcartBO> shopcartList = JsonUtils.jsonToList(shopcartJson,ShopcartBO.class);
Iterator<ShopcartBO> iterator = shopcartList.iterator();
while (iterator.hasNext()){
String ss = iterator.next().getSpecId();
if(ss.equals(itemSpecId)){
iterator.remove();
}