zoukankan      html  css  js  c++  java
  • Java基础知识强化之集合框架笔记07:Collection集合的遍历之迭代器遍历

    1. Collection的迭代器:

    1 Iterator  iterator():迭代器,集合的专用遍历方式

    2. 代码示例:

    package cn.itcast_03;
    
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Iterator;
    
    /*
     * Iterator iterator():迭代器,集合的专用遍历方式
    * Iterator(迭代器): *     Object next():获取元素,并移动到下一个位置。 *       NoSuchElementException:没有这样的元素,因为你已经找到最后了。 *     boolean hasNext():如果仍有元素可以迭代,则返回 true。
    */ public class IteratorDemo { public static void main(String[] args) { // 创建集合对象 Collection c = new ArrayList(); // 创建并添加元素 // String s = "hello"; // c.add(s); c.add("hello"); c.add("world"); c.add("java"); // Iterator iterator():迭代器,集合的专用遍历方式 Iterator it = c.iterator(); // 实际返回的肯定是子类对象,这里是多态 // Object obj = it.next(); // System.out.println(obj); // System.out.println(it.next()); // System.out.println(it.next()); // System.out.println(it.next()); // System.out.println(it.next()); // 最后一个不应该写,所以,我们应该在每次获取前,如果有一个判断就好了 // 判断是否有下一个元素,有就获取,没有就不搭理它 // if (it.hasNext()) { // System.out.println(it.next()); // } // if (it.hasNext()) { // System.out.println(it.next()); // } // if (it.hasNext()) { // System.out.println(it.next()); // } // if (it.hasNext()) { // System.out.println(it.next()); // } // if (it.hasNext()) { // System.out.println(it.next()); // } // 最终版代码 while (it.hasNext()) { // System.out.println(it.next()); String s = (String) it.next(); System.out.println(s); } } }

    运行结果如下:

  • 相关阅读:
    Go语言基础之map
    Go语言基础之切片
    Go语言基础之数组
    Go语言fmt.Printf使用指南
    Go语言基础之流程控制
    Go语言基础之运算符
    Go语言基础之变量和常量
    Go语言环境搭建
    随笔
    使用SocketServer 创建TCP服务端
  • 原文地址:https://www.cnblogs.com/hebao0514/p/4851498.html
Copyright © 2011-2022 走看看