// Using a for-each for loop on an Iterable object. import java.util.*; // This class supports iteration of the // characters that comprise a string. class StrIterable implements Iterable, Iterator { private String str; private int count = 0; StrIterable(String s) { str = s; } // The next three methods impement Iterator. 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(); } // This method implements Iterable. public Iterator iterator() { return this; } } class ForEachIterable { public static void main(String args[]) { StrIterable x = new StrIterable("This is a test."); // Show each character. for(char ch : x) System.out.print(ch); System.out.println(); } }