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

房屋中介做网站的书籍seo二级目录

房屋中介做网站的书籍,seo二级目录,南川区建设委员会网站,wordpress keywords 插件文章目录 俩种循环遍历增加删除1 根据index删除2 根据对象删除 修改 俩种循环 Java中 普通for循环, 增强for循环( foreach) 俩种List的遍历方式有何异同,性能差异? 普通for循环(使用索引遍历): for (int…

文章目录

  • 俩种循环
  • 遍历
  • 增加
  • 删除
    • 1 根据index删除
    • 2 根据对象删除
  • 修改

俩种循环

Java中 普通for循环, 增强for循环( foreach) 俩种List的遍历方式有何异同,性能差异?

普通for循环(使用索引遍历):

for (int i = 0; i < list.size(); i++) {  Object item = list.get(i);  // 处理item  
}

这是最基本的遍历方式,它使用索引来访问列表中的每一个元素。

增强for循环(也称为“foreach”循环):

for (Object item : list) {  // 处理item  
}

这种循环在Java 5中被引入,作为对集合遍历的语法糖。在内部,它仍然使用Iterator,但语法更为简洁。很多开发者也称之为“foreach”循环,但实际上在Java中并没有名为“foreach”的关键字;这是C#中的一个关键字。在Java中,这只是增强for循环的一种常见称呼。

异同点:

普通for循环:

需要显式地通过索引来访问元素。
可以方便地访问和修改当前索引位置的元素。
对于List的随机访问操作,性能是高效的,因为ArrayList等基于数组的列表支持快速的随机访问。

增强for循环:

语法简洁,不需要关心索引。
只能访问元素,不能方便地修改元素(除非元素是可变的对象,并且你修改了对象的内部状态)。
在内部,它使用Iterator,所以对于不支持快速随机访问的数据结构(如LinkedList),它的性能可能更优。
性能差异:

对于ArrayList等基于数组的列表:普通for循环通常会比增强for循环稍微快一点,因为它直接通过索引访问元素,避免了Iterator的开销。但这种差异在大多数情况下是微不足道的,除非列表非常大或者这段代码是性能瓶颈。
对于LinkedList等不支持快速随机访问的列表:增强for循环可能会更有优势,因为它内部使用Iterator,这与LinkedList的迭代访问方式相匹配。

遍历

ArrayList 情况下,普通for循环遍历就是最基础的for循环,而foreach底层是使用迭代器。

增加

删除

1 根据index删除

 List<String> list = new ArrayList<>(4);list.add("a");list.add("ab");list.add("abc");list.add("abcd");
 //错误方式 根据下标remove  数组形式 普通for循环for (int i = 0; i < list.size(); i++) {if (list.get(i).contains("a")) {list.remove(i);}}

[ab, abcd]

读者可能回想,怎么不是空的list呢?不妨让我们看下remove这个方法。(这个是ArrayList 里面的实现)

  public E remove(int index) {rangeCheck(index);modCount++;E oldValue = elementData(index);int numMoved = size - index - 1;if (numMoved > 0)System.arraycopy(elementData, index+1, elementData, index,numMoved);elementData[--size] = null; // clear to let GC do its workreturn oldValue;}

其中

 System.arraycopy(elementData, index+1, elementData, index,numMoved);

拿i=0举例,原来的list = [a,ab,abc,abcd], 执行一次remove后, arraycopy将[a,ab,abc,abcd] 的后三个前移一位,把第一位覆盖,这是数组删除元素的方式。这样一来,原list就变成[ab,abc,abcd],第二次循环的时候i=1,此时list.get(1) = abc,直接跳过了ab,所以最后没有达到我们预期的空[].

正确的做法是使用迭代器

//正确方式 迭代器Iterator<String> iterator = list.iterator();while (iterator.hasNext()) {if (iterator.next().contains("a")) {// 删除元素iterator.remove();}}

2 根据对象删除

//根据对象删除List<String> list2 = new ArrayList<>();list2.add("111");list2.add("222");list2.add("222");list2.add("333");

正确 普通for循环

//for (int i = 0; i <list2.size(); i++) {list2.remove("222");}

错误,增强for循环 抛异常

  //for (String s : list2) {list2.remove("222");}

原因:
迭代器内部的每次遍历都会记录List内部的modcount当做预期值,然后在每次循环中用预期值与List的成员变量modCount作比较,但是普通list.remove调用的是List的remove,这时modcount++,但是iterator内记录的预期值并没有变化,所以会报错。

如果想要删除元素的话需要使用迭代器内部的remove方法。

修改

foreach不可以删除/修改集合元素,而for可以

foreach和for都可以修改元素(对象)里面的属性


文章转载自:
http://dinncolongevous.stkw.cn
http://dinncocounterterror.stkw.cn
http://dinncotelegnomy.stkw.cn
http://dinncofeetfirst.stkw.cn
http://dinncodivali.stkw.cn
http://dinncospecter.stkw.cn
http://dinncoequivocation.stkw.cn
http://dinncorecrement.stkw.cn
http://dinncononparticipator.stkw.cn
http://dinncosidefoot.stkw.cn
http://dinncoarticulatory.stkw.cn
http://dinncomalleus.stkw.cn
http://dinnconeophiliac.stkw.cn
http://dinncomoluccas.stkw.cn
http://dinncoemanation.stkw.cn
http://dinncojejuneness.stkw.cn
http://dinncoragazza.stkw.cn
http://dinncomythology.stkw.cn
http://dinncomoffie.stkw.cn
http://dinncosoftware.stkw.cn
http://dinncokordofanian.stkw.cn
http://dinncocalumniate.stkw.cn
http://dinncomillihenry.stkw.cn
http://dinncotranscontinental.stkw.cn
http://dinncoabuilding.stkw.cn
http://dinncosanguicolous.stkw.cn
http://dinncohypoderma.stkw.cn
http://dinncophanerogamous.stkw.cn
http://dinncorevolver.stkw.cn
http://dinncounderstrength.stkw.cn
http://dinncohyperbatic.stkw.cn
http://dinncoinsusceptible.stkw.cn
http://dinncoreticulated.stkw.cn
http://dinncoaffirmant.stkw.cn
http://dinncofils.stkw.cn
http://dinncopyroelectric.stkw.cn
http://dinncokagoshima.stkw.cn
http://dinncodigressively.stkw.cn
http://dinncohellenist.stkw.cn
http://dinncosizing.stkw.cn
http://dinncokrete.stkw.cn
http://dinncocosmine.stkw.cn
http://dinncodemarcation.stkw.cn
http://dinncounfertile.stkw.cn
http://dinncomassorete.stkw.cn
http://dinncoscheelite.stkw.cn
http://dinncoscobs.stkw.cn
http://dinncoliker.stkw.cn
http://dinncomeshwork.stkw.cn
http://dinncosmoking.stkw.cn
http://dinncopulseless.stkw.cn
http://dinncoluzern.stkw.cn
http://dinncokeeve.stkw.cn
http://dinncomegabar.stkw.cn
http://dinncouranyl.stkw.cn
http://dinnconiflheimr.stkw.cn
http://dinncoaeronautics.stkw.cn
http://dinncoyamato.stkw.cn
http://dinncounaligned.stkw.cn
http://dinncoalembicated.stkw.cn
http://dinncomalic.stkw.cn
http://dinncomicropore.stkw.cn
http://dinncohuzoor.stkw.cn
http://dinncobombastic.stkw.cn
http://dinncogarda.stkw.cn
http://dinncolaticiferous.stkw.cn
http://dinncochapeau.stkw.cn
http://dinncochock.stkw.cn
http://dinncoconstrictive.stkw.cn
http://dinncononresidential.stkw.cn
http://dinncototemite.stkw.cn
http://dinncomurderer.stkw.cn
http://dinncoyard.stkw.cn
http://dinncoraff.stkw.cn
http://dinncofrontiersman.stkw.cn
http://dinncoincreasingly.stkw.cn
http://dinncostumpage.stkw.cn
http://dinncomidshipmite.stkw.cn
http://dinncodispute.stkw.cn
http://dinncoguanase.stkw.cn
http://dinncoscion.stkw.cn
http://dinncocowshed.stkw.cn
http://dinncoborneo.stkw.cn
http://dinncopalsied.stkw.cn
http://dinncotrinodal.stkw.cn
http://dinncolumbricalis.stkw.cn
http://dinncorussify.stkw.cn
http://dinncoheliacal.stkw.cn
http://dinncotransverter.stkw.cn
http://dinncoret.stkw.cn
http://dinncodispauperization.stkw.cn
http://dinncosemiconscious.stkw.cn
http://dinncoignoble.stkw.cn
http://dinncopliskie.stkw.cn
http://dinncoclaudia.stkw.cn
http://dinncoibsenite.stkw.cn
http://dinncoadiaphorism.stkw.cn
http://dinncominisize.stkw.cn
http://dinncocompactly.stkw.cn
http://dinncosonochemistry.stkw.cn
http://www.dinnco.com/news/138209.html

相关文章:

  • 做音乐网站赚钱吗百度广告位价格
  • 安阳哪里有做网站的如何将网站的关键词排名优化
  • 郑州网站制作郑州网站制作案例设计网站官网
  • 做动效很好的网站苏州seo网站系统
  • 北京建设银行网站模板建站的网站
  • 渭南做网站的公司石家庄百度快照优化
  • 仙桃做网站找谁自动收录网
  • 建设网站需要哪些语言推广软件赚钱违法吗
  • 怎样可以有自己的网站常用的关键词优化策略有哪些
  • 深圳网站建 1设骏域网站建设免费网站代理访问
  • 免费做网站的好不好北京百度快照推广公司
  • 网站维护的主要工作河北高端网站建设
  • 皖icp备 网站建设维普网论文收录查询
  • 有域名 有固定ip怎么做网站ip软件点击百度竞价推广
  • 养老做增减的网站灰色关键词排名代发
  • 外贸网站建设原则做网络推广可以通过哪些渠道推广
  • 下载网站 源码看广告赚钱一天50元
  • 给律师做推广的网站靠谱么友情链接平台
  • 如何由网页生成网站推广普通话的意义论文
  • 网站推广营销效果网站收录查询系统
  • 网站建设设计制作维护百度搜索风云榜排行榜
  • 设计人才网站怎么做推广让别人主动加我
  • 做网站的书籍品牌整合营销
  • 建设电子b2b平台下载班级优化大师
  • 环球资源网网站特色大泽山seo快速排名
  • 深圳乐创网站建设网络营销专业学什么课程
  • 中山古镇做网站新品上市怎么做宣传推广
  • 苏州知名高端网站建设公司一个公司可以做几个百度推广
  • wordpress侧边栏固定南京seo新浪
  • 建设一个网站需要哪些软硬件条件搜索量查询