java Iterator 的用法

java.util package has public interface Iterator and contains three methods:

  1. boolean hasNext(): It returns true if Iterator has more element to iterate.
  2. Object next(): It returns the next element in the collection until the hasNext()method return true. This method throws ‘NoSuchElementException’ if there is no next element.
  3. void remove(): It removes the current element in the collection. This method throws ‘IllegalStateException’ if this function is called before next( ) is invoked.

// Java code to illustrate the use of iterator

import java.io.*;

import java.util.*;

class Test {

    public static void main(String[] args)

    {

        ArrayList<String> list = new ArrayList<String>();

        list.add("A");

        list.add("B");

        list.add("C");

        list.add("D");

        list.add("E");

        // Iterator to traverse the list

        Iterator iterator = list.iterator();

        System.out.println("List elements : ");

        while (iterator.hasNext())

            System.out.print(iterator.next() + " ");

        System.out.println();

    }

}

ListIterator

‘ListIterator’ in Java is an Iterator which allows users to traverse Collection in both direction. It contains the following methods:

  1. void add(Object object): It inserts object immediately before the element that is returned by the next( ) function.
  2. boolean hasNext( ): It returns true if the list has a next element.
  3. boolean hasPrevious( ): It returns true if the list has a previous element.
  4. Object next( ): It returns the next element of the list. It throws ‘NoSuchElementException’ if there is no next element in the list.
  5. Object previous( ): It returns the previous element of the list. It throws ‘NoSuchElementException’ if there is no previous element.
  6. void remove( ): It removes the current element from the list. It throws ‘IllegalStateException’ if this function is called before next( ) or previous( ) is invoked.

// Java code to illustrate the use of ListIterator

import java.io.*;

import java.util.*;

class Test {

    public static void main(String[] args)

    {

        ArrayList<String> list = new ArrayList<String>();

        list.add("A");

        list.add("B");

        list.add("C");

        list.add("D");

        list.add("E");

        // ListIterator to traverse the list

        ListIterator iterator = list.listIterator();

        // Traversing the list in forward direction

        System.out.println("Displaying list elements in forward direction : ");

        while (iterator.hasNext())

            System.out.print(iterator.next() + " ");

        System.out.println();

        // Traversing the list in backward direction

        System.out.println("Displaying list elements in backward direction : ");

        while (iterator.hasPrevious())

            System.out.print(iterator.previous() + " ");

        System.out.println();

    }

}

 

详见https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html

// Java code to illustrate the use of ListIterator

import java.io.*;

import java.util.*;

class Test {

    public static void main(String[] args)

    {

        ArrayList<String> list = new ArrayList<String>();

        list.add("A");

        list.add("B");

        list.add("C");

        list.add("D");

        list.add("E");

        // ListIterator to traverse the list

        ListIterator iterator = list.listIterator();

        // Traversing the list in forward direction

        System.out.println("Displaying list elements in forward direction : ");

        while (iterator.hasNext())

            System.out.print(iterator.next() + " ");

        System.out.println();

        // Traversing the list in backward direction

        System.out.println("Displaying list elements in backward direction : ");

        while (iterator.hasPrevious())

            System.out.print(iterator.previous() + " ");

        System.out.println();

    }

}

 

原文地址:https://www.cnblogs.com/jiml/p/9357021.html

时间: 2024-08-30 08:39:17

java Iterator 的用法的相关文章

Iterator的用法

Java中Iterator的用法 迭代器(Iterator):提供一个方法访问一个容器(container)对象中各个元素,而又不需暴露该对象的内部细节! Iterator内有三种方法: 1.Boolean hasNext();      如果仍有元素可以迭代,则返回true 2.Object  next();                 返回迭代的下一个元素 3.void remove();               从迭代器指向的collection中移除迭代器返回的最后一个元素 代码

JAVA的continue用法

JAVA的continue用法: public class test{ public static void main(String [] args){  for(int i=0;i<=10;i++){   if(i%2!=0){    continue;      }   System.out.println(i);    } }} 其实很简单,continue就是中断本次循环开始下一次. 结合这段代码就是.当i是奇数时,不打印,直接结束循环,开始下一次. 还一个例子: //测试continu

汉诺塔(-) java modPow 的用法;

汉诺塔(一) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 在印度,有这么一个古老的传说:在世界中心贝拿勒斯(在印度北部)的圣庙里,一块黄铜板上插着三根宝石针.印度教的主神梵 天在创造世界的时候,在其中一根针上从下到上地穿好了由大到小的64片金片,这就是所谓的汉诺塔.不论白天黑夜,总有一个僧侣在按照下面的法则移动这些金 片:一次只移动一片,不管在哪根针上,小片必须在大片上面.僧侣们预言,当所有的金片都从梵天穿好的那根针上移到另外一根针上时,世界就将在一声霹雳中消

JAVA正则表达式高级用法(分组与捕获)

正则表达式在字符串处理中经常使用,关于正则简单的用法相信有一点程序基础的人都懂得一些,这里就不介绍简单基础了.这里主要讲解一下在JAVA中实现了的正则的高级用法-分组与捕获.对于要重复单个字符,非常简单,直接在字符后卖弄加上限定符即可,例如 a+ 表示匹配1个或一个以上的a,a?表示匹配0个或1个a.这些限定符如下所示: X ?     X ,一次或一次也没有X *     X ,零次或多次X +     X ,一次或多次X { n }     X ,恰好 n 次X { n ,}     X ,

C++ Iterator迭代器介绍及Iterator迭代器用法代码举例

C++ Iterator迭代器介绍 迭代器可被用来访问一个容器类的所包函的全部元素,其行为像一个指针.举一个例子,你可用一个迭代器来实现对vector容器中所含元素的遍历.有这么几种迭代器如下: 迭代器 描述 input_iterator 提供读功能的向前移动迭代器,它们可被进行增加(++),比较与解引用(*). output_iterator 提供写功能的向前移动迭代器,它们可被进行增加(++),比较与解引用(*). forward_iterator 可向前移动的,同时具有读写功能的迭代器.同

JAVA中ArrayList用法

JAVA中ArrayList用法 2011-07-20 15:02:03|  分类: 计算机专业 |  标签:java  arraylist用法  |举报|字号 订阅 Java学习过程中做题时,用到ArrayList,在网上寻找到的学习资料.   摘自:     http://www.cnblogs.com/skylaugh/archive/2006/09/15/505346.html System.Collections.ArrayList类是一个特殊的数组.通过添加和删除元素,就可以动态改变

java 线程基本用法

java中的多线程 一.      java 线程基本介绍 1.进程与线程的区别 进程是指一个内存中运行的应用程序,每个进程都有一块独立的内存空间,一个进程包含一到多个线程. 每个线程都有他所属的进程,每个线程也就是该进程的一条执行路径,线程之间是高频率快速轮换执行的,‘同时’执行只是给人的感觉. 2.Java当中线程一般有5中状态 创建状态:生成线程对象,并没有调用该对象的start方法,这是线程处于创建状态. 就绪状态:当调用了线程的start方法,线程就进入就绪状态,调用start方法后线

Java properties文件用法

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 package com.suyang.properties; import java.io.FileInputStream; import java.io.FileNotFoundException;

Java数组简单用法

数组是具有相同数据类型的一组数据的集合,Java支持多为数组,一维数组的每个基本单元都是基本数据类型的数据,二维数组就是每个基本单元是一维数组的一维数组,以此类推,n维数组的每个基本单元都是n-1为数组的n-1维数组.下面以一维数组为例说明Java数组的用法. 1.数组声明 数组声明有如下两种形式(方括号的位置不同): int arr[]; int[] arr2; 2.数组初始化 数组初始化也有两种形式,如下(使用new或不使用new): int arr[] = new int[]{1, 3,