Java by API/java.util/Iterator

Материал из Java эксперт
Перейти к: навигация, поиск

implements Iterator < Character >

import java.util.Iterator;
import java.util.NoSuchElementException;

class IterableString implements Iterable<Character>, 
                             Iterator<Character> { 
  private String str; 
  private int count = 0; 
 
  IterableString(String s) { 
    str = s; 
  } 
 
  public boolean hasNext() { 
    if(count < str.length()) return true; 
    return false; 
  } 
 
  public Character next() { 
    if(count == str.length())  
      throw new NoSuchElementException(); 
 
    count++; 
    return str.charAt(count-1); 
  } 
 
  public void remove() { 
    throw new UnsupportedOperationException(); 
  } 
 
  public Iterator<Character> iterator() { 
    return this; 
  } 
} 
 
public class MainClass {  
  public static void main(String args[]) {  
    IterableString x = new IterableString("This is a test."); 
 
    for(char ch : x) 
      System.out.print(ch); 
 
    System.out.println(); 
  }  
}





Iterator: hasNext()

/**
 *Output: 
Original contents of al: C A E B D F 
Modified list backwards: F+ D+ B+ E+ A+ C+ 
 */
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
public class MainClass {
  public static void main(String args[]) {
    ArrayList<String> al = new ArrayList<String>();
    al.add("C");
    al.add("A");
    al.add("E");
    al.add("B");
    al.add("D");
    al.add("F");
    System.out.print("Original contents of al: ");
    Iterator<String> itr = al.iterator();
    while (itr.hasNext()) {
      String element = itr.next();
      System.out.print(element + " ");
    }
    System.out.println();
    ListIterator<String> litr = al.listIterator();
    while (litr.hasNext()) {
      String element = litr.next();
      litr.set(element + "+");
    }
    // Now, display the list backwards.
    System.out.print("Modified list backwards: ");
    while (litr.hasPrevious()) {
      String element = litr.previous();
      System.out.print(element + " ");
    }
  }
}





Iterator: next()

/**
 *Output: 
Original contents of al: C A E B D F 
Modified list backwards: F+ D+ B+ E+ A+ C+ 
 */
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
public class MainClass {
  public static void main(String args[]) {
    ArrayList<String> al = new ArrayList<String>();
    al.add("C");
    al.add("A");
    al.add("E");
    al.add("B");
    al.add("D");
    al.add("F");
    System.out.print("Original contents of al: ");
    Iterator<String> itr = al.iterator();
    while (itr.hasNext()) {
      String element = itr.next();
      System.out.print(element + " ");
    }
    System.out.println();
    ListIterator<String> litr = al.listIterator();
    while (litr.hasNext()) {
      String element = litr.next();
      litr.set(element + "+");
    }
    // Now, display the list backwards.
    System.out.print("Modified list backwards: ");
    while (litr.hasPrevious()) {
      String element = litr.previous();
      System.out.print(element + " ");
    }
  }
}