Java Iterable implement
import java.util.Iterator; import java.util.NoSuchElementException; class IterableString implements Iterable<Character>, Iterator<Character> { private String str; private int count = 0; public IterableString(String s) { str = s;/*from w w w. j a v a2 s .c o m*/ } // The next three methods implement 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<Character> iterator() { return this; } } public class Main { public static void main(String args[]) { IterableString x = new IterableString("demo from demo2s.com"); for (char ch : x){ System.out.println(ch); } } }