不错呦!smile@林凯西,确保“准备文件”中的几个文件都有安装,S...您好,看了您这篇帖子觉得很有帮助。但是有个问题想请...我的修改过了怎么还被恶意注册呢 @jjjjiiii 用PJ快9年了,主要是A...PJ3啊,貌似很少有人用PJ了,现在不是WP就是z...@332347365,我当时接入时错误码没有-10...楼主,ChkValue值应为-103是什么意思呢?...大哥 你最近能看到我发的信息,请跟我联系,我有个制...
ArrayList,List,Hashtable,Dictionary的应用
编辑:dnawo 日期:2008-07-04
ArrayList的应用(System.Collections)
List<T>的应用(System.Collections.Generic)
Hashtable的应用(System.Collections)
Dictionary<TKey, TValue>的应用(System.Collections.Generic)
说明:如果存储的元素是Object类型时,List<T>和Dictionary<TKey, TValue>会优于ArrayList和HashTable,因为在存储或检索值类型时ArrayList和HashTable经常要进行装箱和拆箱。
复制内容到剪贴板
程序代码

ArrayList list = new ArrayList();
list.Add(new Student("aaa", 20));
list.Add(new Student("bbb", 21));
list.Add(new Student("ccc", 22));
for (int i = 0; i < list.Count; i++)
{
Student stu = list[i] as Student;
Console.WriteLine(string.Format("Name:{0} Age:{1}", stu.Name, stu.Age));
}
foreach (Object obj in list)
{
Student stu = obj as Student;
Console.WriteLine(string.Format("Name:{0} Age:{1}", stu.Name, stu.Age));
}
list.Add(new Student("aaa", 20));
list.Add(new Student("bbb", 21));
list.Add(new Student("ccc", 22));
for (int i = 0; i < list.Count; i++)
{
Student stu = list[i] as Student;
Console.WriteLine(string.Format("Name:{0} Age:{1}", stu.Name, stu.Age));
}
foreach (Object obj in list)
{
Student stu = obj as Student;
Console.WriteLine(string.Format("Name:{0} Age:{1}", stu.Name, stu.Age));
}
List<T>的应用(System.Collections.Generic)
复制内容到剪贴板
程序代码

List<Student> list = new List<Student>();
list.Add(new Student("aaa", 20));
list.Add(new Student("bbb", 21));
list.Add(new Student("ccc", 22));
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine(string.Format("Name:{0} Age:{1}", list[i].Name, list[i].Age));
}
foreach (Student stu in list)
{
Console.WriteLine(string.Format("Name:{0} Age:{1}", stu.Name, stu.Age));
}
list.Add(new Student("aaa", 20));
list.Add(new Student("bbb", 21));
list.Add(new Student("ccc", 22));
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine(string.Format("Name:{0} Age:{1}", list[i].Name, list[i].Age));
}
foreach (Student stu in list)
{
Console.WriteLine(string.Format("Name:{0} Age:{1}", stu.Name, stu.Age));
}
Hashtable的应用(System.Collections)
复制内容到剪贴板
程序代码

Hashtable hs = new Hashtable();
hs.Add("aaa", 20);
hs.Add("bbb", 21);
hs.Add("ccc", 22);
foreach (DictionaryEntry de in hs)
{
Console.WriteLine(string.Format("Key:{0} Value:{1}",de.Key,de.Value));
}
hs.Add("aaa", 20);
hs.Add("bbb", 21);
hs.Add("ccc", 22);
foreach (DictionaryEntry de in hs)
{
Console.WriteLine(string.Format("Key:{0} Value:{1}",de.Key,de.Value));
}
Dictionary<TKey, TValue>的应用(System.Collections.Generic)
复制内容到剪贴板
程序代码

Dictionary<string, int> dict = new Dictionary<string, int>();
dict.Add("aaa", 20);
dict.Add("bbb", 21);
dict.Add("ccc", 22);
foreach (KeyValuePair<string, int> kvp in dict)
{
Console.WriteLine(string.Format("Key:{0} Value:{1}", kvp.Key, kvp.Value));
}
dict.Add("aaa", 20);
dict.Add("bbb", 21);
dict.Add("ccc", 22);
foreach (KeyValuePair<string, int> kvp in dict)
{
Console.WriteLine(string.Format("Key:{0} Value:{1}", kvp.Key, kvp.Value));
}
说明:如果存储的元素是Object类型时,List<T>和Dictionary<TKey, TValue>会优于ArrayList和HashTable,因为在存储或检索值类型时ArrayList和HashTable经常要进行装箱和拆箱。






评论: 0 | 引用: 0 | 查看次数: 4153
发表评论
请登录后再发表评论!