当前位置: 首页 > news >正文

学做淘宝网站是骗子吗百度官方下载安装

学做淘宝网站是骗子吗,百度官方下载安装,北京网站建设联系电话,外贸建站模版前天看到一篇文章什么?for循环也会出问题?,里面涉及到在for循环中修改集合,想起来自己刚入行的时候就碰到过类似的问题,于是复现了一下文章中的问题,并试验了其它在循环中修改集合的方法。 底层原理参考什…

前天看到一篇文章什么?for循环也会出问题?,里面涉及到在for循环中修改集合,想起来自己刚入行的时候就碰到过类似的问题,于是复现了一下文章中的问题,并试验了其它在循环中修改集合的方法。

底层原理参考什么?for循环也会出问题?这篇文章的分析

1. 在fori中修改集合

  • 在fori中修改集合,不会抛出异常
  • 在fori中移除元素,部分元素可能会被跳过,无法被遍历到
  • 在fori中添加元素,遍历时元素不会被跳过,但如果添加的元素恰好满足添加元素的条件,可能导致无限循环

代码如下:

import java.util.ArrayList;
import java.util.List;public class TestFor {public static void main(String[] args) {List<Integer> list = new ArrayList<Integer>();list.add(1);list.add(2);list.add(3);list.add(4);list.add(5);System.out.println("*****遍历时移除元素*****");for (int index = 0; index < list.size(); index++) {Integer num = list.get(index);System.out.println("当前遍历:" + num);if (num % 2 == 0) {list.remove(num);System.out.println("移除:" + num);}}list.forEach(o -> System.out.print(o + "\t"));System.out.println();list.clear();list.add(1);list.add(2);list.add(3);list.add(4);list.add(5);System.out.println("*****遍历时添加元素*****");for (int index = 0; index < list.size(); index++) {Integer num = list.get(index);System.out.println("当前遍历:" + num);if (num % 2 == 0) {int addNum = 101 + num; // 让添加进去的addNum为奇数,防止后面都是偶数,导致无限循环list.add(addNum);System.out.println("添加:" + addNum);}}list.forEach(o -> System.out.print(o + "\t"));}
}

运行结果:

*****遍历时移除元素*****
当前遍历:1
当前遍历:2
移除:2
当前遍历:4
移除:4
1    3    5
*****遍历时添加元素*****
当前遍历:1
当前遍历:2
添加:103
当前遍历:3
当前遍历:4
添加:105
当前遍历:5
当前遍历:103
当前遍历:105
1    2    3    4    5    103    105

2. 在迭代器iterator中修改集合

  • 在iterator中通过iterator修改集合(remove),不会抛出异常
  • 在iterator中移除元素,元素不会被跳过,但iterator.remove()前,必须先执行iterator.next(),将next element的索引+1,否则会出现IllegalStateException
  • 使用iterator遍历元素时,跳过iterator直接去修改list,只要修改后再次执行iterator.next(),都会出现ConcurrentModificationException
  • 使用iterator遍历元素时,即便在最后一次遍历中(此时iterator.hasNext()为false)才执行list.add()或list.remove(),也会导致iterator.hasNext()从false变为true

代码如下:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;public class TestFor {public static void main(String[] args) {List<Integer> list = new ArrayList<Integer>();list.add(1);list.add(2);list.add(3);list.add(4);list.add(5);System.out.println("*****遍历时移除元素*****");Iterator<Integer> iterator = list.iterator();while (iterator.hasNext()) {// iterator.remove()前,必须先执行iterator.next(),将next element的索引+1,否则会出现IllegalStateExceptionInteger num = iterator.next();System.out.println("当前遍历:" + num);if (num % 2 == 0) {iterator.remove();System.out.println("移除:" + num);}}list.forEach(o -> System.out.print(o + "\t"));System.out.println();list.clear();list.add(1);list.add(2);list.add(3);list.add(4);list.add(5);System.out.println("*****遍历时修改list,修改后不执行iterator.next()*****");iterator = list.iterator();while (iterator.hasNext()) {Integer num = iterator.next();System.out.println("当前遍历:" + num);if (num == 5) {// list.add()、list.remove()都会导致iterator.hasNext()从false变为trueCollections.sort(list);System.out.println("===>排序");}}list.forEach(o -> System.out.print(o + "\t"));System.out.println();list.clear();list.add(1);list.add(2);list.add(3);list.add(4);list.add(5);System.out.println("*****遍历时修改list,修改后执行iterator.next()*****");iterator = list.iterator();while (iterator.hasNext()) {Integer num = iterator.next();System.out.println("当前遍历:" + num);if (num == 3) {Collections.sort(list);System.out.println("===>排序");}}list.forEach(o -> System.out.print(o + "\t"));}
}

运行结果:

*****遍历时移除元素*****
当前遍历:1
当前遍历:2
移除:2
当前遍历:3
当前遍历:4
移除:4
当前遍历:5
1    3    5
*****遍历时修改list,修改后不执行iterator.next()*****
当前遍历:1
当前遍历:2
当前遍历:3
当前遍历:4
当前遍历:5
===>排序
1    2    3    4    5
*****遍历时修改list,修改后执行iterator.next()*****
当前遍历:1
当前遍历:2
当前遍历:3
===>排序
Exception in thread "main" java.util.ConcurrentModificationExceptionat java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1013)at java.base/java.util.ArrayList$Itr.next(ArrayList.java:967)at http.TestFor.main(TestFor.java:56)

3. 在foreach中修改集合

查看编译后的.class后可知,foreach只是迭代器iterator的语法糖,编译后也是用iterator遍历,所以跟在迭代器iterator中修改集合一样

  • 在foreach中没有提供修改list的接口,在非最后一次遍历中修改list会出现ConcurrentModificationException
  • 使用foreach遍历元素时,只在最后一次遍历中修改list(不执行list.add()或list.remove(),而是其它操作比如排序),不会出现ConcurrentModificationException
  • 使用foreach遍历元素时,即便在最后一次遍历中才执行list.add()或list.remove(),也会出现ConcurrentModificationException

代码如下:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;public class TestFor {public static void main(String[] args) {List<Integer> list = new ArrayList<Integer>();list.add(1);list.add(2);list.add(3);list.add(4);list.add(5);System.out.println("*****遍历时修改list,最后一次遍历时修改(不涉及list.add()、list.remove())*****");for (Integer num : list) {System.out.println("当前遍历:" + num);if (num == 5) {// 查看编译后的.class后可知,foreach只是iterator的语法糖,编译后也是用iterator遍历// 如果在最后一次遍历中执行list.add()或list.remove(),会导致ConcurrentModificationExceptionCollections.sort(list);System.out.println("===>排序");}}list.forEach(o -> System.out.print(o + "\t"));System.out.println();System.out.println("*****遍历时修改list,在非最后一次遍历时修改*****");for (Integer num : list) {System.out.println("当前遍历:" + num);if (num == 3) {Collections.sort(list);System.out.println("===>排序");}}list.forEach(o -> System.out.print(o + "\t"));}
}

运行结果:

*****遍历时修改list,最后一次遍历时修改(不涉及list.add()、list.remove())*****
当前遍历:1
当前遍历:2
当前遍历:3
当前遍历:4
当前遍历:5
===>排序
1    2    3    4    5
*****遍历时修改list,在非最后一次遍历时修改*****
当前遍历:1
当前遍历:2
当前遍历:3
===>排序
Exception in thread "main" java.util.ConcurrentModificationExceptionat java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1013)at java.base/java.util.ArrayList$Itr.next(ArrayList.java:967)at http.TestFor.main(TestFor.java:27)

总结

  • 尽量不要在遍历中修改集合本身(修改集合中的元素的属性没问题),除非你能明确知道该操作导致的后果。
  • 如果需要在循环中移除元素,可以使用迭代器iterator。

文章转载自:
http://adagiettos.c7491.cn
http://vsf.c7491.cn
http://hypopyon.c7491.cn
http://cacophonist.c7491.cn
http://balloonist.c7491.cn
http://infarction.c7491.cn
http://tiu.c7491.cn
http://noblesse.c7491.cn
http://overchoice.c7491.cn
http://redneck.c7491.cn
http://toxigenesis.c7491.cn
http://deboost.c7491.cn
http://umiak.c7491.cn
http://saunter.c7491.cn
http://buccaneer.c7491.cn
http://canna.c7491.cn
http://spurgall.c7491.cn
http://arbor.c7491.cn
http://gairish.c7491.cn
http://vinous.c7491.cn
http://castanets.c7491.cn
http://uninspected.c7491.cn
http://hydrotropism.c7491.cn
http://unpriceable.c7491.cn
http://madrilena.c7491.cn
http://radiogeology.c7491.cn
http://consanguine.c7491.cn
http://independentista.c7491.cn
http://hair.c7491.cn
http://leishmania.c7491.cn
http://semiprofessional.c7491.cn
http://anarch.c7491.cn
http://nonbusiness.c7491.cn
http://cubature.c7491.cn
http://intermontane.c7491.cn
http://fragility.c7491.cn
http://lothringen.c7491.cn
http://prefactor.c7491.cn
http://thegn.c7491.cn
http://pulmonary.c7491.cn
http://multiresistant.c7491.cn
http://congelative.c7491.cn
http://taboo.c7491.cn
http://starter.c7491.cn
http://adventurist.c7491.cn
http://hispaniola.c7491.cn
http://colorful.c7491.cn
http://flickertail.c7491.cn
http://signans.c7491.cn
http://wayside.c7491.cn
http://dialect.c7491.cn
http://kwangtung.c7491.cn
http://kinkily.c7491.cn
http://haematopoiesis.c7491.cn
http://oid.c7491.cn
http://ergotoxine.c7491.cn
http://cameralism.c7491.cn
http://fin.c7491.cn
http://definiens.c7491.cn
http://octopodes.c7491.cn
http://muscalure.c7491.cn
http://hypertape.c7491.cn
http://harsh.c7491.cn
http://sanatory.c7491.cn
http://ecumenopolis.c7491.cn
http://displume.c7491.cn
http://pizazz.c7491.cn
http://detestable.c7491.cn
http://flinders.c7491.cn
http://sess.c7491.cn
http://spinet.c7491.cn
http://troophorse.c7491.cn
http://monosaccharide.c7491.cn
http://hammered.c7491.cn
http://reapparition.c7491.cn
http://laundress.c7491.cn
http://starvation.c7491.cn
http://hydrobromic.c7491.cn
http://balneary.c7491.cn
http://floater.c7491.cn
http://aberglaube.c7491.cn
http://eutrapelia.c7491.cn
http://capework.c7491.cn
http://auxotroph.c7491.cn
http://semisweet.c7491.cn
http://overtime.c7491.cn
http://valley.c7491.cn
http://melanie.c7491.cn
http://rps.c7491.cn
http://aia.c7491.cn
http://imposure.c7491.cn
http://regardant.c7491.cn
http://invitee.c7491.cn
http://nervy.c7491.cn
http://encyclic.c7491.cn
http://aaui.c7491.cn
http://cabalism.c7491.cn
http://biodynamic.c7491.cn
http://vespertilionine.c7491.cn
http://laqueus.c7491.cn
http://www.zhongyajixie.com/news/70071.html

相关文章:

  • 可以接单做3d网站市场调研报告怎么写
  • 哈尔滨做网站公司合肥seo
  • 有建设网站的软件吗发帖子的网站
  • 深圳企业企业网站建设电商运营培训课程
  • 武汉网站制作公司哪家好公司企业网站模板
  • 哪里有南宁网站建设丈哥seo博客工具
  • 深圳建网站哪家公司好销售的技巧与口才
  • 公司做网站怎么构思优化推广网站淄博
  • 网站的ftp账号和密码是什么黄页大全
  • 网站广告动态图怎么做河北百度seo
  • 邯郸市地图高清版最新windows优化大师官方免费
  • 国外婚纱网站建设现状济南网站建设制作
  • 做外贸选取哪个网站安卓aso优化
  • 网站建设推推蛙百度人工申诉客服电话
  • 大型网站集群怎么做青岛网站建设哪家好
  • 个人建设网站流程图成都百度推广电话
  • 静态页面做网站网站怎么才能被百度收录
  • 宿迁做网站需要多少钱最新疫情新闻100字
  • 太原中小企业网站制作seo网站怎么搭建
  • 如何做网站帮别人赚钱凡科网站建设
  • 网站结构逻辑结构全国疫情高峰感染高峰进度
  • 美橙互联网站市场调研的内容
  • 淘宝网站建设合同搜索引擎营销的方法有哪些
  • wordpress如何设置css样式表青岛seo推广
  • 新手做网站应该注意什么百度官方优化指南
  • 襄阳网站建设搜索引擎优化规则
  • 网站无法打开网页是怎么回事推广方式都有哪些
  • 网站建设接外包流程图中国企业网络营销现状
  • 西宁最新通告今天广州网站设计专注乐云seo
  • 门户网站上的广告怎么做网站性能优化