查看: 600|回复: 0
打印 上一主题 下一主题

[其他] C#中集合ArrayList和泛型List,Hashtable和Dictionary

[复制链接]
may    

8830

主题

81

听众

7万

积分

首席设计师

Rank: 8Rank: 8

纳金币
52352
精华
343

最佳新人 热心会员 灌水之王 活跃会员 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2016-2-29 22:13:47 |只看该作者 |倒序浏览

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
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-8-17 10:12 , Processed in 0.068546 second(s), 31 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部