1.HashTable

 


        private static void HashTableSample()
        {
            
// Creates and initializes a new Hashtable.

            Hashtable table = new Hashtable();
            table.Add(
"1","frist"
);
            table.Add(
"2","second"
);
            table.Add(
"3","third"
);
            
// Displays the properties and values of the Hashtable.

            Console.WriteLine( "table" );
            Console.WriteLine( 
"  Count:    {0}"
, table.Count );
            Console.WriteLine( 
"  Keys and Values:"
 );
            
//how to print out its keys and values

            foreach(DictionaryEntry d in table)
            {
                Console.WriteLine (
"{0}\t{1}"
, d.Key, d.Value);
            }
            Console.ReadLine();
        }

2.ArrayList

 


private static void ArrayListSample()
        {
            
// Creates and initializes a new ArrayList.

            ArrayList myAL = new ArrayList();
            myAL.Add(
"Hello"
);
            myAL.Add(
"World"
);
            myAL.Add(
"!"
);

            
// Displays the properties and values of the ArrayList.

            Console.WriteLine( "myAL" );
            Console.WriteLine( 
"\tCount:    {0}"
, myAL.Count );
            Console.WriteLine( 
"\tCapacity: {0}"
, myAL.Capacity );
            Console.Write( 
"\tValues:"
 );
            
foreach(object o in
 myAL)
            {
                Console.WriteLine(o.ToString());
            }
            Console.ReadLine();

        }

3.Queue

 


private static void QueueSmaple()
        {
            
// Creates and initializes a new Queue.

            Queue myQ = new Queue();
            myQ.Enqueue(
"Hello"
);
            myQ.Enqueue(
"World"
);
            myQ.Enqueue(
"!"
);

            
// Displays the properties and values of the Queue.

            Console.WriteLine( "myQ" );
            Console.WriteLine( 
"\tCount:    {0}"
, myQ.Count );
            Console.Write( 
"\tValues:"
 );
            System.Collections.IEnumerator myEnumerator 
=
 myQ.GetEnumerator();
            
while
 ( myEnumerator.MoveNext() )
                Console.Write( 
"\t{0}"
, myEnumerator.Current );
            Console.WriteLine();
            Console.ReadLine();
        }

4.自定义集合类:

 


    public class Employee
    {
        
private int
 _id;
        
private string
 _name;
        
public Employee(int id,string
 name)
        {
            _id 
=
 id;
            _name 
=
 name;
        }
        
public int
 id
        {
            
get

            {
                
return _id;
            }
            
set

            {
                _id 
= value;
            }
        }
        
public string
 name
        {
            
get

            {
                
return _name;
            }
            
set

            {
                _name 
= value;
            }
        }
    }
    
public class
 EmployeeCollection :    ICollection
    {
        
private int
 count;
        ArrayList mylist 
= new
 ArrayList();
        
#region 实现接口ICollection

        
public virtual int Count
        {
            
get

            {
                
return count;
            }
        }
        
public virtual bool
 IsSynchronized 
        {
            
get

            {
                
return false;
            }
        }
        
public virtual
 Object SyncRoot 
        {
            
get

            {
                
return this;
            }
        }
        
public virtual void CopyTo(Array array,int
 index)
        {
            
//do nothing

        }
        
public virtual
 IEnumerator GetEnumerator()
        {
            
return null
;
        }
        
#endregion

        
public virtual Employee this[int i]
        {
            
get

            {
                
return (Employee)this.mylist[i]    ;
            }
        }
        
public void AddEmployee(int id,string
 name)
        {
            Employee emp 
= new
 Employee(id,name);
            mylist.Add(emp);
            count 
=
 mylist.Count;
        }
        
public Employee GetEmployee(int
 id)
        {
            
foreach(Employee e in
 mylist)
            {
                
if(id ==
 e.id)
                {
                    
return
 e;
                }
                
else

                {
                    
return null;
                }
            }
            
return null
;
        }
    }

遍历自定义集合类:

 


            EmployeeCollection eCollection = new EmployeeCollection();
            eCollection.AddEmployee(
1,"Engine"
);
            eCollection.AddEmployee(
2,"Water"
);
            eCollection.AddEmployee(
3,"Arvin"
);
            eCollection.AddEmployee(
4,"Vikki"
);
            eCollection.AddEmployee(
5,"Gray"
);
            eCollection.AddEmployee(
6,"Tom"
);
            
for(int i = 0; i <eCollection.Count;i++
)
            {
                Console.WriteLine(eCollection[i].name);
            }
            Console.ReadLine();
posted on 2009-05-30 18:28  钱途无梁  阅读(278)  评论(0编辑  收藏  举报