FileDocCategorySizeDatePackage
Counter.javaAPI DocExample611Mon Nov 13 14:59:00 GMT 2006collections

Counter

public class Counter extends Object implements Iterable

Fields Summary
private int
count
Constructors Summary
public Counter(int count)

 this.count = count; 
Methods Summary
public java.util.Iteratoriterator()

    return new Iterator<Integer>() {
      private int i = 0;
      public boolean hasNext() { return i < count; }
      public Integer next() { i++; return i; }
      public void remove(){ throw new UnsupportedOperationException(); }
    };
  
public static voidmain(java.lang.String[] args)

    int total = 0;
    for (int i : new Counter(3)) {
      total += i;
    }
    assert total == 6;