怎么在Java项目中使用迭代器迭代器遍历集合数据

  介绍

这篇文章将为大家详细讲解有关怎么在Java项目中使用迭代器迭代器遍历集合数据,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

1,使用迭代器遍历ArrayList集合

package  com.jredu.oopch07;   import  java.util.ArrayList;   import  java.util.Iterator;   import 并不知道;   public  class  Ch05  {   ,,,public  static  void  main (String [], args), {   ,,,,,,,//,TODO  Auto-generated  method 存根   ,,,,,,,List  List =, new  ArrayList<在();   ,,,,,,,//集合   ,,,,,,,list.add (1);   ,,,,,,,list.add (2);   ,,,,,,,list.add (3);   ,,,,,,,//迭代器迭代器   ,,,,,,,//1,获取迭代器   ,,,,,,,Iterator  iter =, list.iterator ();   ,,,,,,,//2,通过循环迭代   ,,,,,,,//hasNext():判断是否存在下一个元素   ,,,,,,,,(iter.hasNext ()) {   ,,,,,,,,,,,//如果存在,则调用下一个实现迭代   ,,,,,,,,,,,//对象——在整数——祝辞int   ,,,,,,,,,,,int  j=(int) iter.next();,//把对象型强转成int型   ,,,,,,,,,,,System.out.println (j);   ,,,,,,,}   ,,,}   }

2,使用迭代器遍历集合集合

package  com.jredu.oopch08;   import  java.util.HashSet;   import  java.util.Iterator;   import  java.util.Set;   public  class  Ch01  {   ,,,public  static  void  main (String [], args), {   ,,,,,,,//存储数据的地址   ,,,,,,,Set  Set =, new  HashSet<在();   ,,,,,,,//存储数据   ,,,,,,,set.add (new 主题(1,“标题1“,“简介1“));   ,,,,,,,set.add (new 主题(2,“标题2“,“简介1“));   ,,,,,,,//遍历数据   ,,,,,,,Iterator  iter =, set.iterator ();   ,,,,,,,,(iter.hasNext ()) {   ,,,,,,,,,,,Theme  Theme =,(主题)iter.next ();   ,,,,,,,,,,,System.out.println (theme.getId theme.getTitle () + () + theme.getRemark ());   ,,,,,,,}   ,,,}   }

3,使用迭代器遍历地图集合

package  com.jredu.oopch08;   import  java.util.Collection;   import  java.util.HashMap;   import  java.util.Iterator;   import  java.util.Map;   import  java.util.Set;   public  class  Ch03  {   ,,,public  static  void  main (String [], args), {   ,,,,,,,//,TODO  Auto-generated  method 存根   ,,,,,,,Map  Map =, new  HashMap<在();   ,,,,,,,map.put (1,“a");   ,,,,,,,map.put (2,“b");   ,,,,,,,map.put (3,“c");   ,,,,,,,//所有键值对中的键,组成一个集合   ,,,,,,,Set  Set =, map.keySet ();   ,,,,,,,Iterator  iter =, set.iterator ();   ,,,,,,,,(iter.hasNext ()) {   ,,,,,,,,,,,System.out.println (iter.next());//打印出中映射的键(1、2、3)   ,,,,,,,}   ,,,,,,,//打印出值   ,,,,,,,//价值观所有的值组成的一个集合   ,,,,,,,Collection  col =, map.values ();   ,,,,,,,//重写了toString方法   ,,,,,,,System.out.println (col);//打印出a, b, c的值   ,,,}   } package  com.jredu.oopch08;   import  java.util.Collection;   import  java.util.HashMap;   import  java.util.Iterator;   import  java.util.Map;   import  java.util.Set;   public  class  Ch04  {   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

怎么在Java项目中使用迭代器迭代器遍历集合数据