c#--arrayList实例

例一

//Dictionary
System.Collections.DictionaryEntry dic=new System.Collections.DictionaryEntry("key1","value1");

//ArrayList
System.Collections.ArrayList list=new System.Collections.ArrayList();
list.Add(1);
list.Add(2);
for(int i=0;i<list.Count;i++)
{
 System.Console.WriteLine(list[i]);
}

//HashTable
System.Collections.Hashtable table=new System.Collections.Hashtable();
table.Add("table1",1);
table.Add("table2",2);
System.Collections.IDictionaryEnumerator d=table.GetEnumerator();
while(d.MoveNext())
{
 System.Console.WriteLine(d.Entry.Key);
}

//Queue
System.Collections.Queue queue=new System.Collections.Queue();
queue.Enqueue(1);
queue.Enqueue(2);

System.Console.WriteLine(queue.Peek());
while(queue.Count>0)
{
 System.Console.WriteLine(queue.Dequeue());
}

//SortedList
System.Collections.SortedList list=new System.Collections.SortedList();
list.Add("key2",2);
list.Add("key1",1);
for(int i=0;i<list.Count;i++)
{
 System.Console.WriteLine(list.GetKey(i));
}

//Stack
System.Collections.Stack stack=new System.Collections.Stack();
stack.Push(1);
stack.Push(2);

System.Console.WriteLine(stack.Peek());
while(stack.Count>0)
{
 System.Console.WriteLine(stack.Pop());
}


例二

using System;
using System.Collections;

namespace myCon
{
 class Student
 {
  public Student(){}
  public Student(string strName)
 

  private string name;
  public string Name
  {
   get{ return name; }
  }

  public string ToString()
  {
   return name;
  }
 }

 class Connect: IEnumerable
 {
  IEnum ie = new IEnum();
  public void Add(object obj)
  {
   ie.lst.Add(obj);
  }

  public void ReMove(object obj)
  {
   ie.lst.Remove(obj);
  }

  public IEnumerator GetEnumerator()
  {
   return ie;
  }

  class IEnum: IEnumerator
  {
   public int idx = -1;
   public ArrayList lst = new ArrayList();

   public void Reset()
   {
    idx = -1;
   }

   public object Current
   {
    get
    {
     if (idx>=0 && idx<lst.Count)
      return lst[idx];
     return null;
    }
   }

   public bool MoveNext()
   {
    idx++;
    return idx<lst.Count;
   }
  }

 }
 
 
 class Class1
 {
  static void Main(string[] args)
  {
   Connect con = new Connect();
   con.Add(new Student("aaa"));
   con.Add(new Student("bbb"));
   con.Add(new Student("ccc"));
   foreach(Student stu in con)
   {
    Console.WriteLine(stu.Name);
   }
  
  }
 }
}

 

posted on 2012-03-08 08:49 青蛙學堂 阅读(551) 评论(0)  编辑 收藏 引用 所属分类: 軟件布袋數據庫

只有注册用户登录后才能发表评论。
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

常用链接

留言簿(8)

随笔分类

随笔档案

收藏夹

青蛙学堂

最新评论

阅读排行榜

评论排行榜