您的位置:首页 > 其它

ArrayList,List,HashTable,Dictionary的应用

2008-04-26 14:20 183 查看
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));

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐