码迷,mamicode.com
首页 > 编程语言 > 详细

java Iterator 的用法

时间:2018-07-23 22:05:07      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:inline   move   returns   .com   wing   function   int   import   call   

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();
    }
}

 

java Iterator 的用法

标签:inline   move   returns   .com   wing   function   int   import   call   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!