遍历Collection集合中的6种方法:
下面的代码演示了遍历Collection集合的6种方法,注意Collection集合的遍历远不止于增强for循环,和迭代器两种。
代码如下:
package com.qls.traverse; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack; /**
* 下面是遍历Collection的几种方法,以List接口为例:
* @author 秦林森
*
*/
public class ListTest { public static void main(String[] args) {
// TODO Auto-generated method stub
String[] s="sixi is one of the most beautiful villages in china".split(" ");
List<String> list = Arrays.asList(s);
/**
* 第一种方法用增强for循环。(这里List之所以能用增强for循环其原因在于它实现了Iterable接口)
*/
for(String str:list){
System.out.print(str+" ");
}
System.out.println();
System.out.println("************");
/**
* 第二种方法用Iterator
*/
Iterator<String> it = list.iterator();
while(it.hasNext()){
String next = it.next();
System.out.print(next+" ");
}
System.out.println();
System.out.println("************");
/**
* 第三种方法主要针对LinkedList。因为LinkedList 既有栈(stack)的特点,又有队列(Queue)
* 的特点。所以遍历LinkedList中的元素。根据stack和queue,可以进行相关的遍历。
* 遍历的方法如下所示:
*/
//Using linkedList as a stack
LinkedList<String> list2=new LinkedList<>(list);//创建一个LinkeList包含list中的全部元素。
while(!list2.isEmpty()){
System.out.print(list2.removeFirst()+" ");
}
System.out.println();
System.out.println("************");
/**
* Using linkedList as a queue
*/
LinkedList<String> list3=new LinkedList<>(list);
while(list3.peek() != null){
System.out.print(list3.poll()+" ");
}
System.out.println();
System.out.println("************");
/**
* 第四种方法把所有的Collection都可以当做Enumeration进行遍历
* Collections.enumeration(c)
*/
ArrayList<String> list4=new ArrayList<>(list);
Enumeration<String> e = Collections.enumeration(list4);
while(e.hasMoreElements()){
System.out.print(e.nextElement()+" ");
}
/**第五种方法
* 当然还有其他方法如:
*/
System.out.println();
System.out.println("************");
for(int i=0;i<list4.size();i++){
System.out.print(list4.get(i)+" ");
}
System.out.println();
System.out.println("************");
/**第六种方法:
*再如:
*/
while(!list4.isEmpty()){
int index=0;
System.out.print( list4.remove(index++)+" ");
}
/**
* 备注:在List接口中的所有实现类中最常用的是ArrayList LinkedList
* ArraList比LinkedList的速度快,一般情况下选中ArrayList的情况比LinkedList多。
* 在ArrayList源码中有一个serialVersionUID,这个数字保证了,
* 写入文件(ObjectOutputStream.writeObject(Object))
* 读取文件(ObjectInputStream.readObject())可以顺利进行,
* 并且指明这个数字,可以保持各个版本的兼容性。有利于文件传输。
*/ } }/*Output:
sixi is one of the most beautiful villages in china
************
sixi is one of the most beautiful villages in china
************
sixi is one of the most beautiful villages in china
************
sixi is one of the most beautiful villages in china
************
sixi is one of the most beautiful villages in china
************
sixi is one of the most beautiful villages in china
************
sixi is one of the most beautiful villages in china *///:~
遍历Collection集合中的6种方法:的更多相关文章
- Power BI官方视频(2) Power BI嵌入到应用中的3种方法
今天给大家介绍3种将Power BI嵌入到应用中的方法. 本文原文地址:Power BI官方视频(2) Power BI嵌入到应用中的3种方法 Power BI系列文章地址:微软Power BI技术文 ...
- java.util.Map按照key值合并的value的Collection 集合中。
用java实现把多个map的内容合并的一个resultMap中 代码大致如下 /** * @author Shalf */ public class MapUtil { /** * 把partMa ...
- div盒子水平居垂直中的几种方法
div盒子水平居垂直中的几种方法<!DOCTYPE html><html> <head> <mete charset="ut ...
- 从集合中查找最值得方法——max(),min(),nlargest(),nsmallest()
从集合中查找最值得方法有很多,常用的方法有max(),min(),nlargest(),nsmallest()等. 一.max()和min() 1.1 入门用法 直接使用max(),min(),返回可 ...
- JavaScript确定一个字符串是否包含在另一个字符串中的四种方法
一.indexOf() 1.定义 indexOf()方法返回String对象第一次出现指定字符串的索引,若未找到指定值,返回-1.(数组同一个概念) 2.语法 str.indexOf(searchVa ...
- 【转载】C#中List集合中Last和LastOrDefault方法的差别
在C#的List集合操作中,Last方法和LastOrDefault方法都会用来查找集合中最后一个符合条件的元素对象,但Last和LastOrDefault方法还是有差别的,建议使用LastOrDef ...
- Java Collection集合中的iterator方法
Iterator接口的概述 /** * java.util.Iterator接口:选代器(对集合进行遍历) * 有两个常用的方法 * boolean hasNext() * 如果仍有元素可以迭代,则返 ...
- java中遍历MAP,嵌套map的几种方法
java中遍历MAP的几种方法 Map<String,String> map=new HashMap<String,String>(); map.put("us ...
- java方法中Collection集合的基本使用与方法
集合类的由来,对象用于封闭特有数据,对象多了需要存储,如果对象的个数不确定就使用集合容器进行存储. 集合特点:1.用于存储对象的容器.2.集合的长度是可变的.3.集合中不可以存储基本数据类型值. 集合 ...
随机推荐
- yarn 无法下载node-sass
指定node-sass的下载源 yarn config set sass-binary-site http://npm.taobao.org/mirrors/node-sass
- 课时68.id选择器(掌握)
1.什么是id选择器? 作用:根据指定的id名称找到对应的标签,然后设置属性 格式: #id名称{ 属性:值; } 注意点: 1.每个html标签都有一个属性叫做id,也就是说每个标签都可以设置id ...
- 【转载】谈MongoDB的应用场景
引用:http://blog.csdn.net/adparking/article/details/38727911 MongoDB的应用场景在网上搜索了下,很少介绍关于传统的信息化应用中如何使用Mo ...
- 【jQuery】input框输入手机号自动填充空格
<input type="tel" id="tel"> $("#tel").keyup(function(){ _self = ...
- git 代码托管使用方法
Git代码托管 1 准备材料 在coding,github这些代码托管网站上申请一个账户. Linux平台什么需要一个git,如ubuntu 需要 $ sudo apt-get install git ...
- iar注释快捷键
选中多行后注释快捷键:Ctrl+K 取消多行注释快捷键:Ctrl+Shift+K
- python 函数复习
# 函数 # 可读性强 复用性强 # def 函数名(): # 函数体 #return 返回值 # 所有的函数 只定义不调用就一定不执行 #先定义后调用 #函数名() #不接收返回值 #返回值 = 函 ...
- OC中的block作方法参数时的用法
方式一.在传参时直接声明block回调方法. 1. 定义方法: - (int)doTest:(NSString *)name success:(int (^)(int param1, int para ...
- 2016 ACM-ICPC Asia China-Final D 二分
题意:一共有N个冰淇淋球,做一个冰淇淋需要K个球,并且由于稳定性,这K个球还必须满足上下相邻的下面比上面大至少两倍.先给出N个球的质量,问最多能做出多少个冰淇淋? 思路:二分答案并对其检验. 检验标准 ...
- 004---Django简单示例
一.MVC与MTV模型 在web开发领域里著名的MVC模式,所谓MVC就是把web应用分为模型(M).控制器(C).视图(V)三层,达到了解耦的效果. 一次完整的请求如图: 但是django用的是M ...