四联光电智能照明论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2392|回复: 0
打印 上一主题 下一主题

c# 集合类:ArrayList,StringCollection,Hashtable,List

[复制链接]
  • TA的每日心情
    开心
    2018-7-4 09:08
  • 97

    主题

    392

    帖子

    6095

    积分

    论坛元老

    Rank: 8Rank: 8

    积分
    6095
    跳转到指定楼层
    楼主
    发表于 2016-11-7 21:51:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

    1.数组集合
    其实,在数组的一节里面已经包含了这个概念了。其实数组集合就是 new int[2];
    官方参考地址:http://msdn.microsoft.com/zh-cn/library/57yac89c(VS.80).aspx
    2.ArrayList
    ArrayList跟数组(Array)的区别:http://msdn.microsoft.com/zh-cn/library/41107z8a(VS.80).aspx
    实例:
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Collections;
    6. namespace CSharp
    7. {
    8.      public  class TestArrayList
    9.       {
    10.          public TestArrayList()
    11.           {
    12.              // Create an empty ArrayList, and add some elements.
    13.              ArrayList stringList = new ArrayList();
    14.               stringList.Add("a");
    15.               stringList.Add("abc");
    16.               stringList.Add("abcdef");
    17.               stringList.Add("abcdefg");
    18.               stringList.Add(20);
    19.              // 索引或者说数组下标是数字,所以不需要名字.
    20.              Console.WriteLine("Element {0} is /"{1}/"", 2, stringList[2]);
    21.              // 给下标为2的元素赋值
    22.              stringList[2] = "abcd";
    23.               Console.WriteLine("Element {0} is /"{1}/"", 2, stringList[2]);
    24.              // 输出stringList的总的元素个素
    25.              Console.WriteLine("Number of elements in the list: {0}",
    26.                   stringList.Count);
    27.              try
    28.               {
    29.                  //数组下标从0到count-1,如果尝试输出小于0或者大于等于count的下标,将抛出异常。
    30.                  Console.WriteLine("Element {0} is /"{1}/"",
    31.                       stringList.Count, stringList[stringList.Count]);
    32.               }
    33.              catch (ArgumentOutOfRangeException aoore)
    34.               {
    35.                   Console.WriteLine("stringList({0}) is out of range(越界).",
    36.                       stringList.Count);
    37.               }
    38.              // 不能使用这种方式来增加元素,只能通过stringList.add("aa")来增加元素
    39.             try
    40.               {
    41.                   stringList[stringList.Count] = "42";
    42.               }
    43.              catch (ArgumentOutOfRangeException aoore)
    44.               {
    45.                   Console.WriteLine("stringList({0}) is out of range(越界).",
    46.                       stringList.Count);
    47.               }
    48.               Console.WriteLine();
    49.              //用for来循环
    50.             for (int i = 0; i < stringList.Count; i++)
    51.               {
    52.                   Console.WriteLine("Element {0} is /"{1}/"", i,
    53.                       stringList[i]);
    54.               }
    55.               Console.WriteLine();
    56.              //用foreach来循环
    57.             foreach (object o in stringList)
    58.               {
    59.                   Console.WriteLine(o);
    60.               }
    61.               Console.ReadLine();
    62.           }
    63.       }
    64. }
    复制代码

    这里同时要提到StringCollection,其实这个跟ArrayList没啥区别,只不过StringCollection只能接收字符类型的东西。
    官方地址:http://msdn.microsoft.com/zh-cn/library/system.collections.specialized.stringcollection(VS.80).aspx
    3.List<T> ,这个我想才是我们最常用的。
    官方参考地址:http://msdn.microsoft.com/zh-cn/library/6sh2ey19(VS.80).aspx
    这个其实就是泛型结合数组的例子。
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. namespace CSharp
    6. {
    7.      public  class TestList
    8.       {
    9.          //默认构造函数
    10.         public TestList()
    11.           {
    12.              //声明语法,换句话说就是:定义objAppleList - 集合变量的语法。
    13.              List<Apple> objAppleList = new List<Apple>();
    14.              //定义3个Apple类的实例(也叫对象)
    15.              Apple objApple1 = new Apple();
    16.               objApple1.Color = "red";
    17.               objApple1.Weight = 10;
    18.               Apple objApple2 = new Apple();
    19.               objApple2.Color = "green";
    20.               objApple2.Weight = 12;
    21.               Apple objApple3 = new Apple();
    22.               objApple3.Color = "black";
    23.               objApple3.Weight = 8;
    24.              //把3个Apple类的实例 干到 objAppleList里面去。
    25.              objAppleList.Add(objApple1);
    26.               objAppleList.Add(objApple2);
    27.               objAppleList.Add(objApple3);
    28.              //遍历objAppleList这个集合.
    29.             foreach (Apple o in objAppleList)
    30.               {
    31.                   Console.WriteLine("Color is {0},Weight is {1}", o.Color, o.Weight);
    32.               }
    33.              //总的个数:
    34.              Console.WriteLine("objAppleList的总个数是:{0}", objAppleList.Count);
    35.               Console.ReadLine();
    36.           }
    37.       }
    38.      public  class Apple
    39.       {
    40.          //定义字段
    41.         private string _color = "";
    42.          private decimal _weight = 0;
    43.          //定义跟字段对应的属性
    44.         public string Color
    45.           {
    46.              get { return _color; }
    47.              set { _color = value; }  //这里的value是C#关键字。表示外面传入的值.
    48.          }
    49.          public decimal Weight
    50.           {
    51.              get { return _weight; }
    52.              set { _weight = value; }
    53.           }
    54.       }
    55. }
    复制代码

    在这里:List<Apple> objAppleList = new List<Apple>();,其实我们用数组也可以,如:Apple[] objAappArray = new Apple[3]; 但是数组的限制就是固定了大小。不能动态增加。
    这里为什么不用ArrayList? 按道理,用ArrayList也可以,如:
    我们不用的ArrayList的目的是保证类型安全。因为这个时候,你还可以obAppleArrayList.Add("string");,obAppleArrayList.Add("heihei");,这样obAppleArrayList的元素就不是单纯的Apple类了。
    我们最常用的也是List<T>.
    4.Hashtable,
    官方地址:http://msdn.microsoft.com/zh-cn/library/system.collections.hashtable(VS.80).aspx
    实例:
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Collections;
    6. namespace CSharp
    7. {
    8.      public  class TestHashtable
    9.       {
    10.          public TestHashtable()
    11.           {
    12.               Hashtable objHashtable = new Hashtable();
    13.             
    14.              //需要注意的是:这里的add有点不同于ArrayList,这里需要指定两个值,一个是key,一个value.
    15.              //而且必须都是Object
    16.              objHashtable.Add("Key", "Value");
    17.               objHashtable.Add(1, 2);
    18.               objHashtable.Add(2.1, 3.2);
    19.              //获取所有的key
    20.              ICollection keys = objHashtable.Keys;
    21.              foreach (object key in keys)
    22.               {
    23.                   Console.WriteLine("Key is {0},Values is {1}",key,objHashtable[key]);
    24.               }
    25.               Console.WriteLine();
    26.              //换一种遍历方式:
    27.             foreach (DictionaryEntry de in objHashtable )
    28.               {
    29.                   Console.WriteLine("Key is {0},Values is {1}", de.Key, de.Value);
    30.               }
    31.               Console.ReadLine();
    32.           }
    33.       }
    34. }
    复制代码
    1. ArrayList obAppleArrayList = new ArrayList();
    2.               obAppleArrayList.Add(objApple1);
    3.               obAppleArrayList.Add(objApple2);
    4.               obAppleArrayList.Add(objApple2);
    复制代码


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

    本版积分规则

    QQ|Archiver|手机版|小黑屋|Silian Lighting+ ( 蜀ICP备14004521号-1 )

    GMT+8, 2024-5-19 13:41 , Processed in 1.078125 second(s), 24 queries .

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

    快速回复 返回顶部 返回列表