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

专业购物网站建设价格铁岭建设银行网站

专业购物网站建设价格,铁岭建设银行网站,网站推广常用方法包括,个人网站建站源码今天在测试代码的时候出现一个异常ConcurrentModificationException#xff0c;该异常网上很多解决方案以及解释#xff0c;但我还是再记录一遍吧。代码抽象出来是这样的#xff1a;import java.util.ArrayList;import java.util.List;public class Test {public static voi…今天在测试代码的时候出现一个异常ConcurrentModificationException该异常网上很多解决方案以及解释但我还是再记录一遍吧。代码抽象出来是这样的import java.util.ArrayList;import java.util.List;public class Test {public static void main(String[] args) {List listnew ArrayList();list.add(1);list.add(2);list.add(3);list.add(4);list.add(5);for (Integer i : list) {//这是迭代if(i3){list.remove(new Integer(i));//引起异常的操作}}}}该代码在运行期间就出现java.util.ConcurrentModificationException异常。这个循环其实是对list进行迭代。1.在迭代的时候怎么判断是否还有下一个(hasNext()方法怎么实现)public boolean hasNext() {return cursor ! size();}cursor:Index of element to be returned by subsequent call to nextsize():是该list的size所以只要两者不相等就认为还有元素。2.迭代的时候怎么取下一个(next()方法怎么实现)public E next() {checkForComodification();try {E next get(cursor);lastRet cursor;return next;} catch (IndexOutOfBoundsException e) {checkForComodification();throw new NoSuchElementException();}}final void checkForComodification() {if (modCount ! expectedModCount)throw new ConcurrentModificationException();}modelCount:The number of times this list has been structurally modified.Structural modifications are those that change the size of the list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.expectedModCount:期望的modelCount这2个变量是有迭代器自己来维护的。上面这段程序出现异常是因为我们使用Collection里面的remove方法进行删除ArrayList的remove方法实现public boolean remove(Object o) {if (o null) {for (int index 0; index size; index)if (elementData[index] null) {fastRemove(index);return true;}} else {for (int index 0; index size; index)if (o.equals(elementData[index])) {fastRemove(index);return true;}}return false;}private void fastRemove(int index) {modCount; //***int numMoved size - index - 1;if (numMoved 0)System.arraycopy(elementData, index1, elementData, index,numMoved);elementData[--size] null; // Let gc do its work}modCount1,导致modCount和expectedModCount不相等。3.解决方法就是用迭代器自己的remove方法public void remove() {if (lastRet -1)throw new IllegalStateException();checkForComodification();try {AbstractList.this.remove(lastRet); //将modCount1实现如下if (lastRet cursor)cursor--;lastRet -1;expectedModCount modCount; //维护} catch (IndexOutOfBoundsException e) {throw new ConcurrentModificationException();}}public E remove(int index) {RangeCheck(index);modCount; //***E oldValue (E) elementData[index];int numMoved size - index - 1;if (numMoved 0)System.arraycopy(elementData, index1, elementData, index,numMoved);elementData[--size] null; // Let gc do its workreturn oldValue;}
http://www.zqtcl.cn/news/417810/

相关文章:

  • 网站开发过什么软件杭州cms建站模板下载
  • 做中东服装有什么网站谁能给我个网址
  • 深圳网站建设公司服务平台河北省建设银行网站首页
  • 新密网站网页版微信小程序在哪
  • 长春建一个网站大概要多少钱微信公众号开店流程
  • 网站运营实例django 微信小程序开发教程
  • wordpress 搭网站郑州网站建设华久
  • 网站建设图片尺寸专门做音乐的网站
  • 株洲做网站外贸推广产品
  • 枫叶的网站建设博客企业网站样板制作
  • 织梦网站文章相互调用百度下载老版本
  • pc网站的优势网站建设款属不属于无形资产
  • 网站建设数据安全分析网络全案推广
  • 网站建设哪家好就推 鹏博资讯手机自助建网站
  • 沈阳网站制作招聘网长治网站建设电话
  • 承德网站设计公司余姚做百度网站
  • 阿里云购买网站空间做网站怎么插音乐循环
  • 网站设计时应考虑哪些因素惠州做网站
  • 西安网站搭建的公司网站建设条例
  • 网站建设联系网站改备案信息吗
  • 建设一个看电影的网站唐山网址建站
  • 呼和浩特网站建设价格vs网站开发入门
  • 中国农业工程建设协会网站有专业做线切割配件的网站吗
  • 东莞建网站公司哪个好陕西手机网站建设公司
  • 网站系统里不能打印西安哪有学做淘宝网站
  • 哈尔滨建站模板大全天猫购买
  • 去后台更新一下网站百度最新版下载
  • 盐城网站开发教育建设网站
  • 目前网站开发有什么缺点广东品牌网站建设968
  • 东营做网站优化哪家好简单网站的制作