遍历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.集合中不可以存储基本数据类型值. 集合 ...
随机推荐
- Swoole 创建服务
1: 创建TCP 服务器 $serv = new swoole_server(‘127.0.0.1’,9501); 2:创建UDP服务器 $serv = new swoole_server('127 ...
- 微信小程序相关
https://www.cnblogs.com/shenzikun1314/p/7805168.html
- Requests库:python实现的简单易用的http库
1.get请求: get(url, params, headers) 2.json 解析 3.content 获取二进制内容 4.headers 添加 5.post请求:post(url,data,h ...
- decltype和新的返回值语法
新的返回值语法 让我们讲一下新的返回值语法,这个语法还能看到auto的另一个用处.在以前版本的C和C++中,返回值的类型必须写在函数的前面: int multiply(int x, int y) 在C ...
- strchr函数的用法
原型: char *strchr(const char *s,char c); #include<string.h> 查找字符串s中首次出现字符c的位置,返回首次出现c的位置的指针,如果s ...
- POJ:3259-Wormholes(最短路判断负环)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 58153 Accepted: 21747 Descripti ...
- mutable c++
The keyword mutable is used to allow a particular data member of const object to be modified. This i ...
- js常用框架
JS常用框架:jQuery.Prototype.MooTools 参考:w3cshool jQuery jQuery 是目前最受欢迎的 JavaScript 框架. 它使用 CSS 选择器来访问和操作 ...
- 天性 & 如水一般,遇强则强 —— 梦想、行为、性格
开篇声明,我博客中“小心情”这一系列,全都是日记啊随笔啊什么乱七八糟的.如果一不小心点进来了,不妨直接关掉.我自己曾经写过一段时间的日记,常常翻看,毫无疑问我的文笔是很差的,而且心情也是瞬息万变的.因 ...
- 【Kth Smallest Element in a BST 】cpp
题目: Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. ...