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

佛山做网站建设百度下载安装最新版

佛山做网站建设,百度下载安装最新版,网站开发技术对比,今日nba免费高清直播迭代器 Iterator 的理解和相关集合 使用 1、理解 迭代器(Iterator)是设计模式中的一种,它允许程序员遍历容器(例如列表、集合等)中的元素,而无需了解容器底层的实现细节。在编程中,迭代器提供了…

迭代器 Iterator 的理解和相关集合 使用

1、理解

迭代器(Iterator)是设计模式中的一种,它允许程序员遍历容器(例如列表、集合等)中的元素,而无需了解容器底层的实现细节。在编程中,迭代器提供了一种方法来访问一个聚合对象中的各个元素,而又不暴露该对象的内部表示。

2、特点

  1. 顺序访问:迭代器通常按照某种顺序(如插入顺序或排序顺序)访问容器中的元素。
  2. 一次访问一个元素:迭代器在给定时刻只指向容器中的一个元素。
  3. 状态保存:迭代器保存了遍历容器时的状态,使得在迭代过程中可以记住当前的位置。
  4. 不修改容器:迭代器通常不修改容器本身,只用于访问元素。

3、使用

1、foreach迭代器(增强for循环)

package com.xx.iterator01;import java.util.ArrayList;public class Test01 {/***深入迭代器 -- foreach*/public static void main(String[] args) {// 创建一个ArrayList集合并添加一些元素ArrayList<String> list = new ArrayList<>();//往集合里面添加元素list.add("aaa");list.add("bbb");list.add("ccc");list.add("ddd");list.add("eee");//foreach循环// 它声明了一个变量element(类型为String),该变量将在每次迭代中存储list中的下一个元素。list是外部定义的,并假定为包含String元素的某种集合(最常见的是List<String>)。for (String element : list) {System.out.println(element);}/*** 使用foreach循环遍历集合的底层实现:Iterator<String> it = list.iterator(); // 获取迭代器  String element;  //当他有下一个元素时while (it.hasNext()) { // 使用while循环模拟for-each循环  element = it.next(); // 获取下一个元素  System.out.println(element); // 打印元素  }*/}
}

2、Iterator遍历元素

package com.xx.iterator01;import java.util.ArrayList;
import java.util.Iterator;public class Test02 {/*** 深入迭代器 -- Iterator* * 深入:Iterator如何遍历元素*/public static void main(String[] args) {// 创建一个ArrayList集合并添加一些元素ArrayList<String> list = new ArrayList<>();//添加元素list.add("aaa");list.add("bbb");list.add("ccc");list.add("ddd");list.add("eee");//移除元素list.remove("bbb");//获取迭代器Iterator<String> it = list.iterator();//当有下一个元素时,继续循环打印while(it.hasNext()){String element = it.next();System.out.println(element);}}
}

3、使用Iterator遍历元素,遍历到"bbb"时删除该元素

package com.xx.iterator01;import java.util.ArrayList;
import java.util.Iterator;public class Test02 {/*** 知识点:深入迭代器 -- Iterator* * 深入:Iterator如何遍历元素*/public static void main(String[] args) {// 创建一个ArrayList集合并添加一些元素ArrayList<String> list = new ArrayList<>();//添加元素list.add("aaa");list.add("bbb");list.add("ccc");list.add("ddd");list.add("eee");//移除元素list.remove("bbb");//获取迭代器Iterator<String> it = list.iterator();//当有下一个元素时,继续循环打印while(it.hasNext()){ String element = it.next();//如果元素等于bbb,则删除if(element.equals("bbb")){//list.remove(element);/it.remove();}//开始遍历for (String element : list) {System.out.println(element);}}}
}

4、使用ListIterator遍历元素

package com.xx.iterator01;import java.util.ArrayList;
import java.util.ListIterator;public class Test04 {/*** 深入迭代器 -- ListIterator* * 需求:使用ListIterator遍历元素*/public static void main(String[] args) {ArrayList<String> list = new ArrayList<>();list.add("aaa");list.add("bbb");list.add("ccc");list.add("ddd");list.add("eee");ListIterator<String> listIterator = list.listIterator();while(listIterator.hasNext()){String element = listIterator.next();System.out.println(element);}}
}

5、使用ListIterator遍历元素,遍历到"bbb"时删除

package com.xx.iterator01;import java.util.ArrayList;
import java.util.ListIterator;public class Test05 {/*** 深入迭代器 -- ListIterator* * 需求:使用ListIterator遍历元素,遍历到"bbb"时删除*/public static void main(String[] args) {ArrayList<String> list = new ArrayList<>();list.add("aaa");list.add("bbb");list.add("ccc");list.add("ddd");list.add("eee");ListIterator<String> listIterator = list.listIterator();//当下一个位置有元素时,继续循环while(listIterator.hasNext()){//将下一个位置的元素,赋值给elelmentString element = listIterator.next();//如果元素输入为bbb,删除bbbif(element.equals("bbb")){listIterator.remove();}}//开始遍历集合for (String element : list) {System.out.println(element);}}
}

6、使用ListIterator遍历元素,遍历到"bbb"时添加"xyz"

package com.xx.iterator01;import java.util.ArrayList;
import java.util.ListIterator;public class Test06 {/*** 深入迭代器 -- ListIterator* * 需求:使用ListIterator遍历元素,遍历到"bbb"时添加"xyz"*/public static void main(String[] args) {ArrayList<String> list = new ArrayList<>();list.add("aaa");list.add("bbb");list.add("ccc");list.add("ddd");list.add("eee");ListIterator<String> listIterator = list.listIterator();while(listIterator.hasNext()){String element = listIterator.next();//当元素为bbb时,添加xyzif(element.equals("bbb")){listIterator.add("xyz");}}//遍历集合for (String element : list) {System.out.println(element);}}
}

7、使用ListIterator遍历元素,遍历到"bbb"时替换成"xyz"

package com.xx.iterator01;import java.util.ArrayList;
import java.util.ListIterator;public class Test07 {/*** 深入迭代器 -- ListIterator* * 需求:使用ListIterator遍历元素,遍历到"bbb"时替换成"xyz"*/public static void main(String[] args) {ArrayList<String> list = new ArrayList<>();list.add("aaa");list.add("bbb");list.add("ccc");list.add("ddd");list.add("eee");ListIterator<String> listIterator = list.listIterator();while(listIterator.hasNext()){String element = listIterator.next();//当元素为bbb时,设置bbb替换成xyzif(element.equals("bbb")){listIterator.set("xyz");}}//遍历集合for (String element : list) {System.out.println(element);}}
}

8、使用ListIterator指定下标遍历元素

package com.xx.iterator01;import java.util.ArrayList;
import java.util.ListIterator;public class Test08 {/*** 深入迭代器 -- ListIterator* * 需求:使用ListIterator指定下标遍历元素*/public static void main(String[] args) {//新建一个集合ArrayList<String> list = new ArrayList<>();//添加元素list.add("aaa");list.add("bbb");list.add("ccc");list.add("ddd");list.add("eee");//获取迭代器,从下标为1的开始ListIterator<String> listIterator = list.listIterator(1);while(listIterator.hasNext()){String element = listIterator.next();System.out.println(element);}}
}

9、使用ListIterator倒序遍历元素

package com.qf.iterator01;import java.util.ArrayList;
import java.util.ListIterator;public class Test09 {/*** 深入迭代器 -- ListIterator* * 需求:使用ListIterator倒序遍历元素*/public static void main(String[] args) {ArrayList<String> list = new ArrayList<>();list.add("aaa");list.add("bbb");list.add("ccc");list.add("ddd");list.add("eee");//获取一个迭代器,从集合末尾开始ListIterator<String> listIterator = list.listIterator(list.size());//当迭代器中,前一个位置有元素时,继续循环while(listIterator.hasPrevious()){//将前一个元素赋值给elementString element = listIterator.previous();System.out.println(element);}}
}

文章转载自:
http://aeruginous.wghp.cn
http://eyeful.wghp.cn
http://spectrography.wghp.cn
http://greenwing.wghp.cn
http://fluidics.wghp.cn
http://caporegime.wghp.cn
http://rejoice.wghp.cn
http://barker.wghp.cn
http://sower.wghp.cn
http://unseduced.wghp.cn
http://champagne.wghp.cn
http://assemble.wghp.cn
http://recognizant.wghp.cn
http://dependant.wghp.cn
http://childminder.wghp.cn
http://sonication.wghp.cn
http://antisex.wghp.cn
http://acrocarpous.wghp.cn
http://conclude.wghp.cn
http://witling.wghp.cn
http://reelect.wghp.cn
http://ilmenite.wghp.cn
http://cdsl.wghp.cn
http://danceable.wghp.cn
http://gest.wghp.cn
http://monochromical.wghp.cn
http://reasonably.wghp.cn
http://sector.wghp.cn
http://unstudied.wghp.cn
http://tipcat.wghp.cn
http://embourgeoisification.wghp.cn
http://parajournalism.wghp.cn
http://jugoslav.wghp.cn
http://cyborg.wghp.cn
http://chape.wghp.cn
http://reposit.wghp.cn
http://handed.wghp.cn
http://sarcostyle.wghp.cn
http://amaryllidaceous.wghp.cn
http://codein.wghp.cn
http://amish.wghp.cn
http://summertide.wghp.cn
http://carnify.wghp.cn
http://dryest.wghp.cn
http://pulseless.wghp.cn
http://kill.wghp.cn
http://nonsteroid.wghp.cn
http://odourless.wghp.cn
http://leontiasis.wghp.cn
http://ochreous.wghp.cn
http://neologize.wghp.cn
http://afghanistani.wghp.cn
http://gradienter.wghp.cn
http://rockbird.wghp.cn
http://twentyfold.wghp.cn
http://ludditish.wghp.cn
http://specializing.wghp.cn
http://mesial.wghp.cn
http://essence.wghp.cn
http://lampson.wghp.cn
http://syphilous.wghp.cn
http://malaceous.wghp.cn
http://cirrocumulus.wghp.cn
http://rape.wghp.cn
http://immoderacy.wghp.cn
http://kroo.wghp.cn
http://gardenly.wghp.cn
http://flubdub.wghp.cn
http://apsidiole.wghp.cn
http://petroglyph.wghp.cn
http://tuinal.wghp.cn
http://hypodorian.wghp.cn
http://underdrawers.wghp.cn
http://fulfil.wghp.cn
http://submarine.wghp.cn
http://electrophotometer.wghp.cn
http://eventful.wghp.cn
http://carronade.wghp.cn
http://indeliberateness.wghp.cn
http://microdont.wghp.cn
http://lipbrush.wghp.cn
http://diphenylhydantoin.wghp.cn
http://erst.wghp.cn
http://operand.wghp.cn
http://pachalic.wghp.cn
http://clavicorn.wghp.cn
http://uppie.wghp.cn
http://tenner.wghp.cn
http://impracticability.wghp.cn
http://roselle.wghp.cn
http://cyclandelate.wghp.cn
http://carpale.wghp.cn
http://tun.wghp.cn
http://ugsome.wghp.cn
http://amatively.wghp.cn
http://obtund.wghp.cn
http://narcoma.wghp.cn
http://gretchen.wghp.cn
http://forbidding.wghp.cn
http://orthopteran.wghp.cn
http://www.hrbkazy.com/news/91343.html

相关文章:

  • wordpress 更换中文字体贵阳百度seo点击软件
  • 多少钱需要交个人所得税seo常用工具有哪些
  • 打码兔怎么和网站做接口网络宣传怎么做
  • 怎样用网站做淘宝推广女教师遭网课入侵直播录屏曝
  • 乐清 做网站 多少钱营销策划案的模板
  • 网站前台后台大数据查询
  • 做外贸都有哪些好网站做一个公司网站要多少钱
  • wordpress 故障宕机西安seo网络推广
  • 用于网站建设的费用怎么备注在线视频用什么网址
  • 宁夏一站式网站建设河北网站seo策划
  • 装修网站建设方案推广方案100个
  • 用div css做网站第一步怎么给客户推广自己的产品
  • wordpress设置账号公司网站如何seo
  • 济南中桥信息做的小语种网站怎么样视频剪辑培训机构哪个好
  • 网站建设与管理自考本最近一周新闻大事摘抄2022年
  • dreamweaver cc下载网络推广优化网站
  • 采购网站平台网络软文名词解释
  • 网站联系我们怎么做seo优化关键词放多少合适
  • 防盗网站人做清洁企业网站管理
  • 定西兰州网站建设seo标题优化导师咨询
  • 网站应具有的功能模块宁夏百度推广代理商
  • 网站建设总结查指数
  • 哪个网站可以免费做音乐相册选择一个产品做营销方案
  • 有没有专门做兼职的网站企业网络营销推广方案
  • 传奇网页游戏下载seo与sem的区别与联系
  • 视频网站VIP卡怎么做赠品微信小程序
  • 网站建设的五个基本要素谷歌推广怎么做最有效
  • 做网站建设价格关键词推广软件
  • 有哪些做平面设计好的网站营销活动推广方案
  • 广州荔湾做网站全球网站流量查询