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));
            }

posted on 2008-04-26 14:20  王鹏  阅读(380)  评论(0)    收藏  举报

导航