纳金网

标题: C#中集合ArrayList和泛型List,Hashtable和Dictionary [打印本页]

作者: may    时间: 2016-2-29 22:13
标题: C#中集合ArrayList和泛型List,Hashtable和Dictionary

C# 泛型集合之非泛型集合类与泛型集合类的对应:

ArrayList对应List

HashTable对应Dictionary

Queue对应Queue

Stack对应Stack

SortedList对应SortedList

集合是进化的数组,数组有下标和元素,集合有key和value

1.数组是固定大小的,不能伸缩。虽然System.Array.Resize这个泛型方法可以重置数组大小,
但是该方法是重新创建新设置大小的数组,用的是旧数组的元素初始化。随后以前的数组就废弃!而集合却是可变长的
2.数组要声明元素的类型,集合类的元素类型却是object.
3.数组可读可写不能声明只读数组。集合类可以提供ReadOnly方法以只读方式使用集合。
4.数组要有整数下标才能访问特定的元素,然而很多时候这样的下标并不是很有用。集合也是数据列表却不使用下标访问。
很多时候集合有定制的下标类型,对于队列和栈根本就不支持下标访问!

  1. using System;
  2. using System .Collections .Generic ;//泛型
  3. using System .Collections ;//非泛型
  4. namespace gather
  5. {
  6.         class MainClass
  7.         {
  8.                 public static void Main (string[] args)
  9.                 {
  10.                         ArrayList alist = new ArrayList ();//非泛型ArrayList可以传参数为object类型
  11.                         alist .Add (2);
  12.                         alist .Add ("xxx");
  13.                         alist .Add ("s");
  14.                         foreach (object item in alist) {
  15.                                 Console.WriteLine (item);
  16.                         }

  17.                         List <string>slist = new List<string> ();//非泛型List可以传参数为规定类型<string>
  18.                         slist .Add ("xxssx");//添加元素
  19.                         slist .Add ("v");
  20.                         List <int>ilist = new List<int> ();//泛型List可以传参数为规定类型<string>
  21.                         ilist .Add (4);//添加元素
  22.                         ilist .Add (55);
  23.                         foreach (var ite in slist ) {//遍历
  24.                                 Console.WriteLine (ite);
  25.                         }
  26.                         foreach (var item in ilist) {
  27.                                 Console.WriteLine (item);
  28.                         }
  29.                         Hashtable hash = new Hashtable ();//非泛型Hashtable可以传参数为object类型key和object类型value
  30.                         hash .Add (3,"xxxww");
  31.                         hash .Add ("ed",44);
  32.                         hash .Add ("4","w");
  33.                         foreach (DictionaryEntry   item in hash) {//Hashtable 内的每一组对象就是一个DictionaryEntry
  34.                                 Console.WriteLine (item.Key  );
  35.                                 Console.WriteLine (item .Value );
  36.                         }
  37.                         foreach (var item in hash.Keys ) {//遍历key
  38.                                 Console.WriteLine (item);
  39.                         }
  40.                         foreach (var item in hash.Values ) {//遍历value
  41.                                 Console.WriteLine (item);
  42.                         }
  43.                         Dictionary <int ,int >dict =new Dictionary<int, int>();//泛型Dictionary可以传参数为规定类型<int,int>
  44.                         dict .Add (4,3);
  45.                         dict .Add (5,7);
  46.                         dict .Add (0,44);
  47.                         foreach (object   item in dict ) {//遍历每一组对象
  48.                                 Console.WriteLine (item);
  49.                         }
  50.                         foreach (var item in dict.Keys ) {//遍历key值
  51.                                 Console.WriteLine (item);
  52.                         }       
  53.                         foreach (var item in dict.Values ) {//遍历value值
  54.                                 Console.WriteLine (item);
  55.                         }
  56.                 }
  57.         }
  58. }
复制代码

其他集合方法

205718m88ngg83894ml4e4.jpg





欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5