private Iterator<Integer> iter; //Iterator for indexList OR lineList (depends on the call)
//Iterator for index list. Return iterator's hasNext for index list
public bool hasNext()
{
if (!iter.hasNext())
return false;
return true;
}
//Return iterator's next index
public int next()
{
return((int)iter.next()).intValue();
}
กรณี ใช้ Iterator feature กับ class ที่เป็น collection ในตัวอย่างจะเป็น Generic type
[Any object ,any data type] Code (C#)
using System.Collections;
using System.Collections.Generic;
namespace GenericIteratorExample
{
public class Stack<T> : IEnumerable<T>
{
private T[] values = new T[100];
private int top = 0;
public void Push(T t) { values[top++] = t; }
public T Pop() { return values[--top]; }
// These make Stack<T> implement IEnumerable<T> allowing
// a stack to be used in a foreach statement.
public IEnumerator<T> GetEnumerator()
{
for (int i = top; --i >= 0; )
{
yield return values[i];
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
// Iterate from top to bottom.
public IEnumerable<T> TopToBottom
{
get
{
// Since we implement IEnumerable<T>
// and the default iteration is top to bottom,
// just return the object.
return this;
}
}
// Iterate from bottom to top.
public IEnumerable<T> BottomToTop
{
get
{
for (int i = 0; i < top; i++)
{
yield return values[i];
}
}
}
//A parameterized iterator that return n items from the top
public IEnumerable<T> TopN(int n)
{
// in this example we return less than N if necessary
int j = n >= top ? 0 : top - n;
for (int i = top; --i >= j; )
{
yield return values[i];
}
}
}
//This code uses a stack and the TopToBottom and BottomToTop properties
//to enumerate the elements of the stack.
class Test
{
static void Main()
{
Stack<int> s = new Stack<int>();
for (int i = 0; i < 10; i++)
{
s.Push(i);
}
// Prints: 9 8 7 6 5 4 3 2 1 0
// Foreach legal since s implements IEnumerable<int>
foreach (int n in s)
{
System.Console.Write("{0} ", n);
}
System.Console.WriteLine();
// Prints: 9 8 7 6 5 4 3 2 1 0
// Foreach legal since s.TopToBottom returns IEnumerable<int>
foreach (int n in s.TopToBottom)
{
System.Console.Write("{0} ", n);
}
System.Console.WriteLine();
// Prints: 0 1 2 3 4 5 6 7 8 9
// Foreach legal since s.BottomToTop returns IEnumerable<int>
foreach (int n in s.BottomToTop)
{
System.Console.Write("{0} ", n);
}
System.Console.WriteLine();
// Prints: 9 8 7 6 5 4 3
// Foreach legal since s.TopN returns IEnumerable<int>
foreach (int n in s.TopN(7))
{
System.Console.Write("{0} ", n);
}
System.Console.WriteLine();
}
}
}
กรณี implement Iterator feature ลงใน class ใดๆ Code (C#)
using System;
using System.Collections;
public class Person
{
public Person(string fName, string lName)
{
this.firstName = fName;
this.lastName = lName;
}
public string firstName;
public string lastName;
}
public class People : IEnumerable
{
private Person[] _people;
public People(Person[] pArray)
{
_people = new Person[pArray.Length];
for (int i = 0; i < pArray.Length; i++)
{
_people[i] = pArray[i];
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator) GetEnumerator();
}
public PeopleEnum GetEnumerator()
{
return new PeopleEnum(_people);
}
}
public class PeopleEnum : IEnumerator
{
public Person[] _people;
// Enumerators are positioned before the first element
// until the first MoveNext() call.
int position = -1;
public PeopleEnum(Person[] list)
{
_people = list;
}
public bool MoveNext()
{
position++;
return (position < _people.Length);
}
public void Reset()
{
position = -1;
}
object IEnumerator.Current
{
get
{
return Current;
}
}
public Person Current
{
get
{
try
{
return _people[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
}
class App
{
static void Main()
{
Person[] peopleArray = new Person[3]
{
new Person("John", "Smith"),
new Person("Jim", "Johnson"),
new Person("Sue", "Rabon"),
};
People peopleList = new People(peopleArray);
foreach (Person p in peopleList)
Console.WriteLine(p.firstName + " " + p.lastName);
}
}
แต่ถ้าจะสะดวกในการใช้งานน่าจะ inherit จาก data structure ที่ใกล้เคียงกับงานของคุณจะดีกว่าเช่น
List< int > หรือ Hastable<int>