![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections;
using System.Collections.Generic;
public class Node
{
public string Name
{
get;
set;
}
public Node(string s)
{
this.Name=s;
}
}
public class NodeCollection
{
private ArrayList list=new ArrayList();
private int nodeIndex=0;
public void AddNode(Node n)
{
list.Add(n);
nodeIndex++;
}
public Node GetNode(int i)
{
return ((Node)list[i]);
}
public int NodeIndex
{
get
{
return nodeIndex;
}
}
}
public abstract class Iterator
{
public abstract Node Next();
}
public class ReverseIterator:Iterator
{
private NodeCollection nodeCollection;
private int currentIndex;
public ReverseIterator(NodeCollection c)
{
this.nodeCollection=c;
currentIndex=c.NodeIndex-1;
}
public override Node Next()
{
if(currentIndex==-1)
{
return null;
}
else
{
return (nodeCollection.GetNode(currentIndex--));
}
}
}
public class MyClass
{
public static void Main()
{
NodeCollection c=new NodeCollection();
c.AddNode(new Node("first"));
c.AddNode(new Node("Second"));
c.AddNode(new Node("third"));
ReverseIterator i=new ReverseIterator(c);
Node n;
do
{
n=i.Next();
if(n!=null)
{
Console.WriteLine("{0}",n.Name);
}
}
while(n!=null);
}
}
using System.Collections;
using System.Collections.Generic;
public class Node
{
public string Name
{
get;
set;
}
public Node(string s)
{
this.Name=s;
}
}
public class NodeCollection
{
private ArrayList list=new ArrayList();
private int nodeIndex=0;
public void AddNode(Node n)
{
list.Add(n);
nodeIndex++;
}
public Node GetNode(int i)
{
return ((Node)list[i]);
}
public int NodeIndex
{
get
{
return nodeIndex;
}
}
}
public abstract class Iterator
{
public abstract Node Next();
}
public class ReverseIterator:Iterator
{
private NodeCollection nodeCollection;
private int currentIndex;
public ReverseIterator(NodeCollection c)
{
this.nodeCollection=c;
currentIndex=c.NodeIndex-1;
}
public override Node Next()
{
if(currentIndex==-1)
{
return null;
}
else
{
return (nodeCollection.GetNode(currentIndex--));
}
}
}
public class MyClass
{
public static void Main()
{
NodeCollection c=new NodeCollection();
c.AddNode(new Node("first"));
c.AddNode(new Node("Second"));
c.AddNode(new Node("third"));
ReverseIterator i=new ReverseIterator(c);
Node n;
do
{
n=i.Next();
if(n!=null)
{
Console.WriteLine("{0}",n.Name);
}
}
while(n!=null);
}
}
名称 | Iterator |
结构 | ![]() |
意图 | 提供一种方法顺序访问一个聚合对象中各个元素, 而又不需暴露该对象的内部表示。 |
适用性 |
|