用Iterator实现遍历集合
使用Collection类的Iterator,可以方便的遍历Vector, ArrayList, LinkedList等集合元素,避免通过get()方法遍历时,针对每一种对象单独进行编码。
示例:
- Collection coll = new Vector(); //LinkedList(); //ArrayList();
 - coll.add("Tody");
 - coll.add("is");
 - coll.add("Sunday.");
 - // Output all elements by iterator
 - Iterator it = coll.iterator();
 - while(it.hasNext()) {
 - System.out.print(it.next() + " ");
 - }
 
输出:
Tody is Sunday.
1.hasNext()函数的API解释
hasNext
boolean hasNext()
- Returns 
trueif the iteration has more elements. (In other words, returnstrueifnext()would return an element rather than throwing an exception.) 
- Returns:
 trueif the iteration has more elements
---------------------------------------------------------
2.next()函数的API解释
next
E next()
- Returns the next element in the iteration.
 - 
- Returns:
 - the next element in the iteration
 - Throws:
 NoSuchElementException- if the iteration has no more elements
 
- Collection coll = new HashSet();
 - coll.add("Tody");
 - coll.add("is");
 - coll.add("Sunday.");
 - // Output all elements by iterator
 - Iterator it = coll.iterator();
 - while(it.hasNext()) {
 - System.out.print(it.next() + " ");
 - }
 
输出:
is Sunday. Tody
由上面两个例子看出,在List和Set对象中,Iterator的next()方法返回的值是不一样的。
原因是List属于线性集合,元素是有序的,读取时是按照数组的形式,一个接一个的读取,存储也是按照add的顺序添加的。
而Set属于非线性的,是无序的,所以读取的元素与添加的顺序不一定一致。
对于HashSet,其实它返回的顺序是按Hashcode的顺序。
如果迭代也有序,则可以用LinkedHashSet。
http://topic.csdn.net/u/20101227/09/63a23d05-7f15-4b0e-9287-e97f96ba4349.html?77188351
用Iterator实现遍历集合的更多相关文章
- 增强for循环遍历集合或数组
		
遍历:for循环遍历数组或集合:iterator迭代器遍历集合:还有增强for循环(for each)遍历数组或集合: 遍历数组: 遍历集合:
 - 遍历集合的Iterator删除其中的元素
		
package list; import java.util.LinkedList; /* * 遍历集合的时候删除其中的元素 从后往前删,每次都删除的是最后一个元素,不涉及移位 */public cl ...
 - java8 增强的Iterator遍历集合元素
		
Iterator接口也是Java集合框架的成员,与Collection和Map两个系列的集合不一样的是Collection和Map系列主要用于充当容器的作用,而Iterator正如其名字一样是主要用于 ...
 - 如何边遍历集合边删除元素--使用Iterator中的remove()方法
		
在遍历集合时,想将符合条件的某些元素删除,开始是用了下面的方法 public static void main(String[] args) throws UnsupportedEncodingExc ...
 - 迭代器:遍历集合元素的操作. iterator()
		
package seday11; import java.util.ArrayList;import java.util.Collection;import java.util.Iterator; / ...
 - 8.2.2 使用Java8增强的Iterator遍历集合元素
		
8.2.2 使用Java 8增强的Iterator遍历集合元素 Iterator接口方法 程序示例 Iterator仅用于遍历集合 Iterator必须依附于Collection对象 修改迭代变量的值 ...
 - 集合—collection、iterator遍历集合
		
一.collection接口 1.collection常用方法 点击查看代码 @Test public void test(){ //contains() Collection coll = new ...
 - Lambda表达式遍历集合
		
1.Collection Java 8 为Iterable接口新增了一个forEach(Consumer action)默认方法,该方法所需参数的类型是一个函数式接口,而Iterable接口是Coll ...
 - JAVA基础学习之final关键字、遍历集合、日期类对象的使用、Math类对象的使用、Runtime类对象的使用、时间对象Date(两个日期相减)(5)
		
1.final关键字和.net中的const关键字一样,是常量的修饰符,但是final还可以修饰类.方法.写法规范:常量所有字母都大写,多个单词中间用 "_"连接. 2.遍历集合A ...
 
随机推荐
- C#操作摄像头 实现拍照功能
			
从正式工作以来一直做的都是基于B/S的Web开发,已经很长时间不研究C/S的东西了,但是受朋友的委托,帮他做一下拍照的这么个小功能.其实类似的代码网上有很多,但是真的能够拿来运行的估计也没几个.本来是 ...
 - 分享一下自己写的Python 3的各种PDF文档【花了半年时间那】
			
这些文档花了我半年的时间去整理.因为是第一次进行整理,希望帮助后来者少走弯路.毕竟是第一次整理.哪些地方不到位,希望大家和我练习,我们一起把它做好,以下就直接给出下载地址了,都是免积分的下载奥.因此. ...
 - elasticsearch安装与使用(5)-- search guard安装与配置
			
一.安装search guard插件必须要安装两部分: ①search-guard-xx ②search-guard-ssl (XX指的是与elasticsearch引擎对应的版本) github地址 ...
 - openfire数据库mysql配置
			
<?php return array( //'配置项'=>'配置值' //'USERNAME'=>'admin', //赋值 //数据库配置信息 'DB_TYPE' => 'm ...
 - 关于Cocos2d-x中根据分数增加游戏难度的方法
			
1.GameScene.h中声明一些分数边界值 //level提升所需的分数 enum LevelUp_Score { Level1Up_Score = , Level2Up_Score = , Le ...
 - 第二百八十七节,MySQL数据库-条件语句、循环语句、动态执行SQL语句
			
MySQL数据库-条件语句.循环语句.动态执行SQL语句 1.if条件语句 delimiter \\ CREATE PROCEDURE proc_if () BEGIN ; THEN ; ELSEIF ...
 - 关掉firefox(火狐)和palemoon地址栏自动加www.前缀功能【转】
			
常用palemoon调试网站域名,它会很“贴心”的给你输入的网址前加上www.前缀,可有些域名前并没有www前缀,这样就导致了无法打开网站,今天学习下关闭它的这个功能. 打开firefox,在地址栏输 ...
 - The request lifetime scope cannot be created because the HttpContext is not available
			
项目中应用了Autofac,在Global轮询处理Job的时候,需要获取现有得Service,而这些Service已经通过Autofac进行了配置,所以理所应当的用下面的代码去获取了. Depende ...
 - 一直误解的memset函数
			
1.“想当然”导致的后果 今天犯了一个十分低级的错误,在对一个整型数组用memset进行初始化设置所有元素值为1.可是结果却大出所料,很意外啊!接着,我就做了代码测试. #include <io ...
 - win7cmd静态绑定arp
			
netsh -c "172.16.3.1" "f4-ea-67-8b-91-cc"