1 package uk.co.jezuk.mango.iterators; 2 3 7 public class StringIterator implements java.util.Iterator 8 { 9 public StringIterator(String s) 10 { 11 s_ = s; 12 if((s_ != null) && (s_.length() > 0)) 13 pos_ = 0; 14 } 16 public boolean hasNext() 17 { 18 return (pos_ != -1); 19 } 21 public Object next() 22 { 23 String c = s_.substring(pos_, pos_+1); 24 if(++pos_ == s_.length()) 25 pos_ = -1; 26 return c; 27 } 29 public void remove() 30 { 31 throw new UnsupportedOperationException ("uk.co.jezuk.mango.StringIterator does not support the remove method. In fact it's probably a logic error that you called it at all. Strings are immutable"); 32 } 34 private String s_; 35 private int pos_ = -1; 36 } | Popular Tags |