遍历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.集合中不可以存储基本数据类型值. 集合 ...
随机推荐
- linux mysql5.7 安装、 开机启动
一.安装 wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz h ...
- ethereum(以太坊)(十一)--字节数组(一)
pragma solidity ^0.4.0; contract byte1{ /* 固定大小字节数组(Fixed-size byte arrays) 固定大小字节数组可以通过bytes1,bytes ...
- 基于THINKPHP+layui+Ajax无刷新实现图片上传预览
<fieldset class="layui-elem-field" style="width:500px;margin:50px 0 0 300px;" ...
- python的运行过程剖析·编程语言分类
总结: 编程语言的分类 编译型: 说明:与汇编语言类似,都有一个编译程序将源代码编译成硬件可执行的二进制代码 特点:执行速度快.同等情况下对系统要求低,适合于开发大型应用程序.数据库系统.操作系统等 ...
- 中国剩余定理算法详解 + POJ 1006 Biorhythms 生理周期
转载请注明出处:http://exp-blog.com/2018/06/24/pid-1054/ #include <iostream> #include <cstdio> u ...
- python time时间模块
在Python中,通常有这三种方式来表示时间:时间戳.元组(struct_time).格式化的时间字符串 (1)时间戳(timestamp) :通常来说,时间戳表示的是从1970年1月1日00:00: ...
- 算法:枚举法---kotlin
枚举法:效率低,循环所有的情况,找到正确答案 用于解决数学问题,还是很简单的. 比如,奥数里面: 算 法 描 述 题X题=题题题题题题 其中 算法描述题每一个为一个数字,请写出正确的数字. ok,我们 ...
- iOS程序执行顺序和UIViewController 的生命周期(整理)
说明:此文是自己的总结笔记,主要参考: iOS程序的启动执行顺序 AppDelegate 及 UIViewController 的生命周期 UIView的生命周期 言叶之庭.jpeg 一. iOS程序 ...
- 【Soft-Margin Support Vector Machine】林轩田机器学习技术
Hard-Margin的约束太强了:要求必须把所有点都分开.这样就可能带来overfiiting,把noise也当成正确的样本点了. Hard-Margin有些“学习洁癖”,如何克服这种学习洁癖呢? ...
- 去除文件夹中的.svn
一.在Dos窗口中运行如下命令 for/r <你项目的路径> %i in (.svn) do rd /s /q %i 二.将“Delete SVN Folders”操作添加到右击菜单中 建 ...