implements Iterator < Character > : Iterator : java.util : Java by API examples (example source code) Organized by topic

Java by API
C++
PHP


Java by API  »  java.util   » [  Iterator  ]   
 



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


           
       
Related examples in the same category
1.  Iterator: hasNext()
2.  Iterator: next()
























Home| Contact Us
Copyright 2003 - 07 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.