1.集合输出

很多情况下我们需要把集合的内容进行输出,也就是遍历集合。

遍历集合的方式有以下几种:

1.Iterator

2.ListIterator

3.Enumeration(枚举方式,比较老一般不用)

4.foreach

5.传统for循环

其中Iterator的使用率最高。

public class CollectionIterator {

    /**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成的方法存根
iterator();
fors();
foreach();
}
/**
* 使用Iterator迭代
*/
public static void iterator(){
List<Student> list=new ArrayList<Student>();
Student stu1=new Student("小刚",16);
Student stu2=new Student("小花",23);
Student stu3=new Student("小刚",16);
list.add(stu1);
list.add(stu2);
list.add(stu3);
Iterator<Student> i=list.iterator();
while(i.hasNext()){
System.out.println(i.next());
}
}
/**
* 使用for循环迭代
* 针对数组集合来说,使用传统的for遍历效率更高
*/
public static void fors(){
List<Student> list=new ArrayList<Student>();
Student stu1=new Student("小刚",16);
Student stu2=new Student("小花",23);
Student stu3=new Student("小刚",16);
list.add(stu1);
list.add(stu2);
list.add(stu3);
//使用Iterator迭代
int size=list.size();
for(int i=0;i<size;i++){
System.out.println(list.get(i));
}
}
/**
* 使用foreach迭代
*/
public static void foreach(){
List<Student> list=new ArrayList<Student>();
Student stu1=new Student("小刚",16);
Student stu2=new Student("小花",23);
Student stu3=new Student("小刚",16);
list.add(stu1);
list.add(stu2);
list.add(stu3);
for(Student s:list){
System.out.println(s);
}
} }

Java集合——集合框架Iterator接口的更多相关文章

  1. Java类集框架——List接口

    学习目标 掌握List接口与Collection接口的关系. 掌握List接口的常用子类:ArrayList.Vector. 掌握ArrayList与Vector类的区别.    Collection ...

  2. Java API ——Collection集合类 & Iterator接口

    对象数组举例: 学生类: package itcast01; /** * Created by gao on 15-12-9. */ public class Student { private St ...

  3. Java基础 -- Collection和Iterator接口的实现

    Collection是描述所有序列容器(集合)共性的根接口,它可能被认为是一个“附属接口”,即因为要表示其他若干个接口的共性而出现的接口.另外,java.util.AbstractCollection ...

  4. java:类集框架conllection接口list,set

    类集中提供了以下几种接口: 1.单值操作接口:conllection,List,Set list和set是conllection接口的子接口 2.一对值的操作接口:Map 3.排序的操作接口:Sort ...

  5. 详解 迭代器 —— Iterator接口、 ListIterator接口 与 并发修改异常

    (请关注 本人"Collection集合"博文--<详解 Collection集合>) Iterator接口(迭代器): 概述: 对 collection 进行迭代的迭 ...

  6. Java集合类中的Iterator和ListIterator的区别

    注意:内容来自网络他人文章! 最近看到集合类,知道凡是实现了Collection接口的集合类,都有一个Iterator方法,用于返回一个实现了Iterator接口的对象,用于遍历集合:(Iterato ...

  7. java集合 之 Collection和Iterator接口

    Collection是List,Queue和Set接口的父接口,该接口里定义的方法即可用于操作Set集合,也可以用于List和Queue集合.Collection接口里定义了如下操作元素的方法. bo ...

  8. Java之集合初探(二)Iterator(迭代器),collections,打包/解包(装箱拆箱),泛型(Generic),comparable接口

    Iterator(迭代器) 所有实现了Collection接口的容器都有一个iterator方法, 用来返回一个实现了Iterator接口的对象 Iterator对象称作迭代器, 用来方便的实现对容器 ...

  9. Java集合框架顶层接口collectiion接口

    如何使用迭代器 通常情况下,你会希望遍历一个集合中的元素.例如,显示集合中的每个元素. 一般遍历数组都是采用for循环或者增强for,这两个方法也可以用在集合框架,但是还有一种方法是采用迭代器遍历集合 ...

随机推荐

  1. [转] 更新Flash CS6发布设置的目标播放器版本

    目前Aodbe发布的最新版的Flash CS6,都不支持将Flash Player 11作为目标播放器版本发布.这个问题很容易解决,但涉及到的东西却比较多,我在这里将一一讲解.首先来个Setp by ...

  2. POJ3126Prime Path(BFS)

    #include"cstdio" #include"queue" #include"cstring" using namespace std ...

  3. 一 ThreadLocal

    (1)  Threadlocal定义: 当使用ThreadLocal维护变量时,ThreadLocal为每个使用该变量的线程提供独立的变量副本,所以每一个线程都可以独立地改变自己的副本,而不会影响其它 ...

  4. R: 缺失值 & 查看变量类型

    ################################################### 问题:缺失值   18.5.2 有关处理缺失值的各种方法有什么?各自的适用场景. 解决方案: n ...

  5. 21 、GPD-PSL-VCF

    https://genome.ucsc.edu/FAQ/FAQformat.html#format9 1.Variant Call Format(VCF) Example ##fileformat=V ...

  6. 8、scala面向对象编程之Trait

    一.Trait基础 1.将trait作为接口使用 // Scala中的Triat是一种特殊的概念 // 首先我们可以将Trait作为接口来使用,此时的Triat就与Java中的接口非常类似 // 在t ...

  7. Entity Framework Code-First(8):Configure Domain Classes

    Configure Domain Classes in Code-First: We learned default Code-First Conventions in the previous se ...

  8. Other - 个人对知识讨论、分享等平台上抄袭乱象的看法

    在某论坛上看到这样一句话,深表赞同.

  9. Spring-boot 项目中使用 jackson 遇到的一个问题

    jackson介绍 java代码中实现序列化和反序列化的工具类 jackson使用Demo https://github.com/Naylor55/JavaDebrisCode/tree/branch ...

  10. [CentOS7] 挂载iso镜像文件到/media目录下

    声明:本文主要总结自:鸟哥的Linux私房菜-第七章.Linux 磁碟與檔案系統管理,如有侵权,请通知博主 首先,设置虚拟机让它加载iso镜像文件到CD/DVD设备中,这个设备对应于Linux下的/d ...