Java学习笔记

在实现购物车时,不能使用foreach进行迭代,应该使用Iterator

 1   public void delete(String id) {
 2         if (id != null && !"".equals(id)) {
 3             Iterator<CartItem> it = items.iterator();
 4             while (it.hasNext()) {
 5                 CartItem item = it.next();
 6                 if (item.getId().equals(id)) {
 7                     it.remove();//删除当前被迭代的对象
 8                 }
 9             }
10         }
11    }

 

posted on 2016-12-16 16:58  llynic  阅读(89)  评论(0编辑  收藏  举报

导航