ArrayList,List,HashTable,Dictionary的应用

ArrayList的应用:
            ArrayList list  =   new  ArrayList();
            
for  ( int  i  =   0 ; i  <   3 ;i ++  ) 
            {
                student s1 
=   new  student();
                s1.Name 
=   "" + i;
                s1.Age 
=   2   -  i;
                list.Add(s1);
            }
            
for  ( int  i  =   0 ; i  <  list.Count;i ++  ) 
            {
                MessageBox.Show(
string .Format( " 姓名为:{0} " ,((student)list[i]).Name));
            }
            
foreach (Object obj  in  list)
            {
                MessageBox.Show(
string .Format( " 年龄为:{0} " ,((student)obj).Age));
            }

HashTable的应用:
            Hashtable hs  =   new  Hashtable();
            
for  ( int  i  =   0 ; i  <   3 ; i ++ )
            {
                student s1 
=   new  student();
                s1.Name 
=   ""   +  i;
                s1.Age 
=   2   -  i;
                hs.Add(s1.Name,s1.Age);
            }
            
foreach  (Object obj  in  hs.Keys)
            {
                MessageBox.Show(
string .Format( " 姓名为:{0} " , obj));
            }
            
foreach  (Object obj  in  hs.Values)
            {
                MessageBox.Show(
string .Format( " 年龄为:{0} " , obj));
            }

List<obj>的应用:
            List < student >  list  =   new  List < student > ();
            
for  ( int  i  =   0 ; i  <   3 ; i ++ )
            {
                student s1 
=   new  student();
                s1.Name 
=   ""   +  i;
                s1.Age 
=   2   -  i;
                list.Add(s1);
            }
            
for  ( int  i  =   0 ; i  <  list.Count; i ++ )
            {
                MessageBox.Show(
string .Format( " 姓名为:{0} " , list[i].Name));
            }
            
foreach  (student s1  in  list)
            {
                MessageBox.Show(
string .Format( " 年龄为:{0} " , s1.Age));
            }

Dictionary<key,obj>的应用:
            Dictionary < string ,student >  d1  =   new  Dictionary < string ,student > ();
            
for  ( int  i  =   0 ; i  <   3 ; i ++ )
            {
                student s1 
=   new  student();
                s1.Name 
=   ""   +  i;
                s1.Age 
=   2   -  i;
                d1.Add(s1.Name, s1);
            }
            
foreach  ( string  name  in  d1.Keys)
            {
                MessageBox.Show(
string .Format( " 姓名为:{0} " ,name ));
            }
            
foreach  (student s1  in  d1.Values)
            {
                MessageBox.Show(
string .Format( " 年龄为:{0} " , s1.Age));
            }

转载于:https://www.cnblogs.com/wp19861223/archive/2008/04/26/1172136.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值