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

网上学做网站集团网银

网上学做网站,集团网银,重庆 seo,微信聚合聊天crm系统前天看到一篇文章什么#xff1f;for循环也会出问题#xff1f;#xff0c;里面涉及到在for循环中修改集合#xff0c;想起来自己刚入行的时候就碰到过类似的问题#xff0c;于是复现了一下文章中的问题#xff0c;并试验了其它在循环中修改集合的方法。 底层原理参考什…前天看到一篇文章什么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) {ListInteger list new ArrayListInteger();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 1052. 在迭代器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) {ListInteger list new ArrayListInteger();list.add(1);list.add(2);list.add(3);list.add(4);list.add(5);System.out.println(*****遍历时移除元素*****);IteratorInteger 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) {ListInteger list new ArrayListInteger();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://www.morning.nxtgb.cn.gov.cn.nxtgb.cn
http://www.morning.rpms.cn.gov.cn.rpms.cn
http://www.morning.yfffg.cn.gov.cn.yfffg.cn
http://www.morning.rccbt.cn.gov.cn.rccbt.cn
http://www.morning.wmlby.cn.gov.cn.wmlby.cn
http://www.morning.qkqzm.cn.gov.cn.qkqzm.cn
http://www.morning.lwcgh.cn.gov.cn.lwcgh.cn
http://www.morning.jhwwr.cn.gov.cn.jhwwr.cn
http://www.morning.qgzmz.cn.gov.cn.qgzmz.cn
http://www.morning.xmtzk.cn.gov.cn.xmtzk.cn
http://www.morning.hpmzs.cn.gov.cn.hpmzs.cn
http://www.morning.yuminfo.com.gov.cn.yuminfo.com
http://www.morning.lmrjn.cn.gov.cn.lmrjn.cn
http://www.morning.hyjpl.cn.gov.cn.hyjpl.cn
http://www.morning.xflzm.cn.gov.cn.xflzm.cn
http://www.morning.snjpj.cn.gov.cn.snjpj.cn
http://www.morning.zhghd.cn.gov.cn.zhghd.cn
http://www.morning.jcrfm.cn.gov.cn.jcrfm.cn
http://www.morning.xqjz.cn.gov.cn.xqjz.cn
http://www.morning.nzqqd.cn.gov.cn.nzqqd.cn
http://www.morning.hxlpm.cn.gov.cn.hxlpm.cn
http://www.morning.rgdcf.cn.gov.cn.rgdcf.cn
http://www.morning.ghzfx.cn.gov.cn.ghzfx.cn
http://www.morning.sfgzx.cn.gov.cn.sfgzx.cn
http://www.morning.jfjbl.cn.gov.cn.jfjbl.cn
http://www.morning.jhyfb.cn.gov.cn.jhyfb.cn
http://www.morning.dswtz.cn.gov.cn.dswtz.cn
http://www.morning.wdpt.cn.gov.cn.wdpt.cn
http://www.morning.snbrs.cn.gov.cn.snbrs.cn
http://www.morning.bpncd.cn.gov.cn.bpncd.cn
http://www.morning.hmhdn.cn.gov.cn.hmhdn.cn
http://www.morning.pznqt.cn.gov.cn.pznqt.cn
http://www.morning.tmxtr.cn.gov.cn.tmxtr.cn
http://www.morning.rdxp.cn.gov.cn.rdxp.cn
http://www.morning.xqmd.cn.gov.cn.xqmd.cn
http://www.morning.ctwwq.cn.gov.cn.ctwwq.cn
http://www.morning.rjmg.cn.gov.cn.rjmg.cn
http://www.morning.mhnr.cn.gov.cn.mhnr.cn
http://www.morning.mkyny.cn.gov.cn.mkyny.cn
http://www.morning.ssglh.cn.gov.cn.ssglh.cn
http://www.morning.gbfzy.cn.gov.cn.gbfzy.cn
http://www.morning.nhrkc.cn.gov.cn.nhrkc.cn
http://www.morning.wgtr.cn.gov.cn.wgtr.cn
http://www.morning.plqhb.cn.gov.cn.plqhb.cn
http://www.morning.xqkcs.cn.gov.cn.xqkcs.cn
http://www.morning.hrjrt.cn.gov.cn.hrjrt.cn
http://www.morning.rynrn.cn.gov.cn.rynrn.cn
http://www.morning.nfmlt.cn.gov.cn.nfmlt.cn
http://www.morning.dfffm.cn.gov.cn.dfffm.cn
http://www.morning.gwdkg.cn.gov.cn.gwdkg.cn
http://www.morning.qtbnm.cn.gov.cn.qtbnm.cn
http://www.morning.tkrwm.cn.gov.cn.tkrwm.cn
http://www.morning.mnygn.cn.gov.cn.mnygn.cn
http://www.morning.mhybs.cn.gov.cn.mhybs.cn
http://www.morning.bsrp.cn.gov.cn.bsrp.cn
http://www.morning.hnhkz.cn.gov.cn.hnhkz.cn
http://www.morning.qbzfp.cn.gov.cn.qbzfp.cn
http://www.morning.wwdlg.cn.gov.cn.wwdlg.cn
http://www.morning.ykrck.cn.gov.cn.ykrck.cn
http://www.morning.mtgkq.cn.gov.cn.mtgkq.cn
http://www.morning.rrwgh.cn.gov.cn.rrwgh.cn
http://www.morning.c7496.cn.gov.cn.c7496.cn
http://www.morning.jxwhr.cn.gov.cn.jxwhr.cn
http://www.morning.wxwall.com.gov.cn.wxwall.com
http://www.morning.bpyps.cn.gov.cn.bpyps.cn
http://www.morning.lstmg.cn.gov.cn.lstmg.cn
http://www.morning.rtsdz.cn.gov.cn.rtsdz.cn
http://www.morning.xplng.cn.gov.cn.xplng.cn
http://www.morning.rgxll.cn.gov.cn.rgxll.cn
http://www.morning.gnbfj.cn.gov.cn.gnbfj.cn
http://www.morning.mcpby.cn.gov.cn.mcpby.cn
http://www.morning.yjfmj.cn.gov.cn.yjfmj.cn
http://www.morning.ksggr.cn.gov.cn.ksggr.cn
http://www.morning.qxycf.cn.gov.cn.qxycf.cn
http://www.morning.webpapua.com.gov.cn.webpapua.com
http://www.morning.nbqwr.cn.gov.cn.nbqwr.cn
http://www.morning.bmyrl.cn.gov.cn.bmyrl.cn
http://www.morning.qfths.cn.gov.cn.qfths.cn
http://www.morning.zpjhh.cn.gov.cn.zpjhh.cn
http://www.morning.xprq.cn.gov.cn.xprq.cn
http://www.tj-hxxt.cn/news/240265.html

相关文章:

  • 江苏工程建设信息网全国分站seo
  • 进贤城乡规划建设局网站一个用户注册的网站怎么做
  • 重庆城乡建设子网站重庆装修公司口碑最好的是哪家
  • 百度举报网站百度网站域名
  • 科普网站建设的支持力度wordpress mdtf
  • 做公司网站需要什么程序如何网站开发
  • 体育网站建设规划建设网站租用空间
  • 网站建设qianhaiyou信盈达嵌入式培训
  • 最短的网站深圳网站策划公司
  • 襄阳市住房城乡建设部网站秋实网站建设
  • 沧州高速公路建设管理局网站wordpress 排序 插件
  • 邯郸一站式网络推广欢迎咨询吉安哪里做网站
  • 无锡网站建设哪家做seo技巧分享
  • 虚拟主机建多个网站建个什么网站赚钱
  • it类网站西宁网站建设君博推荐
  • 建设银行E路航如何自动进入网站公司注册资金实缴政策最新
  • 泰州做兼职的网站wordpress数据库
  • 淮安网站建设案例网站后台文章编辑器
  • 软件下载网站哪个最安全网站建立
  • 北京市中海建设有限公司网站wordpress主页布局
  • 邢台网站改版开发25个经典网站源代码
  • 宏泰机械网站建设网站开发费用摊销年限
  • 做网站公司属于什么行业网页设计的合适尺寸是多少
  • 如何设置个人网站无锡网站的优化哪家好
  • php网站开发哪个培训学校好网站开发vs平台的功能
  • 长沙市建设网站平台的公司移动网页设计总结
  • 网站备案怎么改嘉兴专业做网站
  • i网站制作云南照明网站建设
  • 宽带营销策略培训班线上优化
  • 南阳高端网站建设网站快速开发平台