用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 ...
随机推荐
- elasticsearch安装与使用(4)-- 安装中文分词插件elasticsearch 的 jdbc
前言 elasticsearch(下面简称ES)使用jdbc连接mysql比go-mysql-elasticsearch的elasticsearch-river-jdbc能够很好的支持增量数据更新的问 ...
- LINQ教程三:Lambda表达式解剖
C#3.0(.NET3.5)中引入了Lambda表达式和LINQ.Lambda表达式是使用一些特殊语法表示匿名方法的较短方法. 最基本的Lambda表达式语法如下: (参数列表)=>{方法体} ...
- 创建ajax对象并兼容多个浏览器方法简单记录
这篇文章主要介绍了如何创建ajax对象并兼容多个浏览器,需要的朋友可以参考下<script> function createAjax(){ var request=false; //win ...
- 不可错过的10个超棒jQuery表单操作代码片段
jQuery 绝对是一个伟大的开源javascript类库,是帮助我们快速和高效开发前端应用的利器.可能大家在日常的开发过程中常常会处理表单相关的 javascript,在今天这篇代码片段分享文章中, ...
- 关于Cocos2d-x中监听物体不超越边界的解决方案
写一个监听器 touchlistener->onTouchMoved = [this](Touch* pTouch, Event*) { auto delta = pTouch->getD ...
- Struts2之Domain Model(域模型)。
使用原因 为了避免在action中有太多的类,而需要写大量的get().set(). 故在Struts2 使用 了 domain model. Action: private User user; p ...
- 【转】C# 调用WebService的方法
很少用C#动态的去调用Web Service,一般都是通过添加引用的方式,这样的话是自动成了代理,那么动态代理调用就是我们通过代码去调用这个WSDL,然后自己去生成客户端代理.更多的内容可以看下面的两 ...
- 用IFrame作预览pdf,图片
<iframe id="my_img" src="@ViewBag.path" width="100%" frameborder=&q ...
- SSL 证书配置nginx
ssl.conf文件: server { listen 443; server_name www.domain.com; # 改为绑定证书的域名 ssl on; ssl_certificate 1_w ...
- schema in oracle
the conception of schema is different in different db software. here i just refer to oracle schema. ...