ArrayList,Hashtable,List<T>,Dictionary<K,V>
1.ArrayList
ArrayList list = new ArrayList(); //for遍历 for (int i = 0; i < list.Count; i++) { SE se=(SE)list[i]; Console.WriteLine(se.Name); } //foreach遍历 foreach(Object obj in list) { SE se=(SE)list[i]; Console.WriteLine(se.Name); }
2.Hashtable
Hashtable list=new Hashtable(); //Hashtable不能用索引访问,所以遍历一个Hashtable只能用foreache()方法 //遍历Key foreach(Object obj in list.Keys) { Console.WriteLine((string)obj); } //遍历Value foreach(Object obj in list.Values) { SE se=(SE)obj; Console.WriteLine(se.Name); } //同时遍历,DictionaryEntry一个结构 foreach(DictionaryEntry en in list){ Console.WriteLine(se.Keys); COnsole.WriteLine(((SE)en.Value).Name); }
3.List<T>
//for遍历 for (int i = 0; i < list.Count; i++) { //遍历时不需要类型转换 Console.WriteLine(list[i]); } //foreach遍历 foreach(SE obj in list) { //遍历时不需要类型转换 Console.WriteLine(obj); } //
4.Dictionary<K,V>
//遍历Values foreach(SE se in list.Values) { //遍历时不需要类型转换 Console.WriteLine(se); } //同时遍历 foreach(KeyValuePair<string,SE> en in list) { Console.WriteLine(en.Key); Console.WriteLine(en.Value.Name); } //KeyValuePair<TKey,TValue>是一个泛型结构