几个常用的集合类的使用

1.HashTable

 

ContractedBlock.gif ExpandedBlockStart.gif Code
        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

 

ContractedBlock.gif ExpandedBlockStart.gif Code
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

 

ContractedBlock.gif ExpandedBlockStart.gif Code
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.自定义集合类:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
    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;
        }
    }

遍历自定义集合类:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
            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();

转载于:https://www.cnblogs.com/qiantuwuliang/archive/2009/05/30/1492366.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值