下面的代码演示了遍历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种方法:的更多相关文章

  1. Power BI官方视频(2) Power BI嵌入到应用中的3种方法

    今天给大家介绍3种将Power BI嵌入到应用中的方法. 本文原文地址:Power BI官方视频(2) Power BI嵌入到应用中的3种方法 Power BI系列文章地址:微软Power BI技术文 ...

  2. java.util.Map按照key值合并的value的Collection 集合中。

    用java实现把多个map的内容合并的一个resultMap中 代码大致如下 /**  * @author Shalf  */ public class MapUtil { /** * 把partMa ...

  3. div盒子水平居垂直中的几种方法

      div盒子水平居垂直中的几种方法<!DOCTYPE html><html>    <head>        <mete charset="ut ...

  4. 从集合中查找最值得方法——max(),min(),nlargest(),nsmallest()

    从集合中查找最值得方法有很多,常用的方法有max(),min(),nlargest(),nsmallest()等. 一.max()和min() 1.1 入门用法 直接使用max(),min(),返回可 ...

  5. JavaScript确定一个字符串是否包含在另一个字符串中的四种方法

    一.indexOf() 1.定义 indexOf()方法返回String对象第一次出现指定字符串的索引,若未找到指定值,返回-1.(数组同一个概念) 2.语法 str.indexOf(searchVa ...

  6. 【转载】C#中List集合中Last和LastOrDefault方法的差别

    在C#的List集合操作中,Last方法和LastOrDefault方法都会用来查找集合中最后一个符合条件的元素对象,但Last和LastOrDefault方法还是有差别的,建议使用LastOrDef ...

  7. Java Collection集合中的iterator方法

    Iterator接口的概述 /** * java.util.Iterator接口:选代器(对集合进行遍历) * 有两个常用的方法 * boolean hasNext() * 如果仍有元素可以迭代,则返 ...

  8. java中遍历MAP,嵌套map的几种方法

    java中遍历MAP的几种方法 Map<String,String> map=new HashMap<String,String>();    map.put("us ...

  9. java方法中Collection集合的基本使用与方法

    集合类的由来,对象用于封闭特有数据,对象多了需要存储,如果对象的个数不确定就使用集合容器进行存储. 集合特点:1.用于存储对象的容器.2.集合的长度是可变的.3.集合中不可以存储基本数据类型值. 集合 ...

随机推荐

  1. yarn 无法下载node-sass

    指定node-sass的下载源 yarn config set sass-binary-site http://npm.taobao.org/mirrors/node-sass

  2. Effective Approaches to Attention-based Neural Machine Translation(Global和Local attention)

    这篇论文主要是提出了Global attention 和 Local attention 这个论文有一个译文,不过我没细看 Effective Approaches to Attention-base ...

  3. Gson杂记录

    //Integer userId = getUserId(); //System.out.println("userId:"+userId); /*for(int i=0;i< ...

  4. 正则表达式(re)

    1.re.match(pattern, str, flag) 从str的第一个字母开始匹配,若不是开头的,尽管属于str内,则无法匹配. 2.贪婪匹配与非贪婪匹配(?) 贪婪匹配:尝试匹配尽可能多的字 ...

  5. linux处理僵尸进程

    由来 在linux下,如果一个进程终止,内核会释放该进程使用的所有存储区,关闭所有文件句柄等,但是,内核会为每个终止子进程保留一定量的信息.这些信息至少包括进程ID,进程的终止状态,以及该进程使用的C ...

  6. HDU 5446 Unknown Treasure (卢卡斯+CRT

    代码: #include"bits/stdc++.h" #define db double #define ll long long #define vec vector<l ...

  7. mysql学习第三天练习(流程控制函数)

    -- 流程控制函数 -- 1.查询员工部门号,并赋予部门名 select empno,ename,deptno,case deptno then '10部门' then '20部门' else '30 ...

  8. Spring---加载配置文件的几种方法(org.springframework.beans.factory.BeanDefinitionStoreException)

    Spring中的几种容器都支持使用xml装配bean,包括:XmlBeanFactory ,ClassPathXmlApplicationContext ,FileSystemXmlApplicati ...

  9. laravel5.5契约

    无规矩不成方圆, Laravel 的契约是一组定义框架提供的核心服务的接口,规定了实现该接口的规范. 为什么要使用接口 首先,让我们来看一些高耦合缓存实现的代码.如下: <?php namesp ...

  10. python学习总结---函数使用 and 装饰器

    # 函数使用 ### 零碎知识 - 灵活的if-else ```python a = 3 if False else 5 print(a) ''' if False: a = 3 else: a = ...