当前位置: 代码迷 >> C# >> ArrayList,Hashtable,List<T>Dictionary<K,V>
  详细解决方案

ArrayList,Hashtable,List<T>Dictionary<K,V>

热度:551   发布时间:2016-04-28 08:23:19.0
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()方法//遍历Keyforeach(Object obj in list.Keys){  Console.WriteLine((string)obj);}//遍历Valueforeach(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>

//遍历Valuesforeach(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>是一个泛型结构

 

  相关解决方案