首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

List的遍历条件剔除

2012-10-25 
List的遍历条件删除List的遍历条件删除ListInteger l new ArrayListInteger()for (int i 0 i

List的遍历条件删除

List的遍历条件删除

List<Integer> l = new ArrayList<Integer>();for (int i = 0; i < 10; i++) {l.add(i);}// 方法1,目标是删除1for (int i = 0; i < l.size(); i++) {if (l.get(i) == 1) {l.remove(i);}}System.out.println(l);// 方法2,目标是删除2Iterator<Integer> it = l.iterator();while (it.hasNext()) {if (it.next() == 2) {it.remove();}}System.out.println(l);// 方法3,目标是删除3(错误)for (Integer i : l) {if (i == 3) {l.remove(i);}}System.out.println(l);

热点排行