zoukankan      html  css  js  c++  java
  • C# 常见的几种集合的应用

    写代码也很多年了,说一说c# 各种集合的比较与应用吧

    1. ArrayList

    ArrayList类似于数组,有人也称它为数组列表。ArrayList可以动态维护,而数组的容量是固定的。它的索引会根据程序的扩展而重新进行分配和调整。和数组类似,它所存储的数据称为元素,它所保存的元素数就是它的容量。默认初始容量为0,在使用它时,需引入命名空间System.Connections;

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace Demo
     7 {
     8     public class Student
     9     {
    10         public Student() { }
    11         public Student(string name, char sex, int age)
    12         {
    13             this.Name = name;
    14             this.Sex = sex;
    15             this.Age = age;
    16         }
    17         public string Name { get; set; }
    18         public char Sex { get; set; }
    19         public int Age { get; set; }
    20     }
    21 
    22 }
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Collections;
     6 using System.Diagnostics;
     7 
     8 namespace Demo
     9 {
    10     class Program
    11     {
    12         static void Main(string[] args)
    13         {
    14             ArrayList array = new ArrayList();//实例化一个ArrayList集合对象
    15             Student Zhang = new Student("张三", '', 22);
    16             Student Li = new Student("李四", '', 20); 
    17             Student Sun = new Student("小五", '', 21);
    18 
    19             Console.WriteLine("**********使用ArrayList 集合*********");
    20             //用ArrayList的对象名添加Student对象
    21             array.Add(Zhang);
    22             array.Add(Li);       
    23             array.Add(Sun);
    24             Console.WriteLine("**********使用For循环遍历**********");
    25            
             for (int i = 0; i < array.Count; i++) 26 { 27 Student stu = (Student)array[i]; 28 Console.WriteLine("我的姓名是{0},年龄是{1},性别是{2}", stu.Name, stu.Age, stu.Sex); 29 } 30 31 Console.WriteLine("删除Student张三的信息"); 32 array.Remove(Zhang);//用对象名删除元素 33 Console.WriteLine(" ***********使用Foreach循环遍历一***********"); 34 35 foreach (Student item in array) 36 { 37 Console.WriteLine("我的姓名是{0},年龄是{1},性别是{2}", item.Name, item.Age, item.Sex); 39 } 40 41 Console.WriteLine(" ***********使用Foreach循环遍历二***********"); 42 foreach (Student item in array) 43 { 44 Student stu = (Student)item; 45 Console.WriteLine("我的姓名是{0},年龄是{1},性别是{2}", stu.Name, stu.Age, stu.Sex); 46 } 47 Console.WriteLine(" ***********使用Foreach循环遍历三***********"); 48 foreach (var item in array) 49 { 50 Student stu = (Student)item; 51 Console.WriteLine("我的姓名是{0},年龄是{1},性别是{2}", stu.Name, stu.Age, stu.Sex); 52 } 53 } 54 } 55 }

    2.HashTable

    C# /提供了一种称为HashTable的数据结构,通常称为哈希表,有的人称它为"字典".HashTable的数据是通过键(Key)和值(Value)来组织的,同ArrayList一样,它也属于System.Collections命名空间中,它所存放的每个元素都是键/值对.

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Collections;
     6 using System.Diagnostics;
     7 
     8 namespace Demo
     9 {
    10     class Program
    11     {
    12         static void Main(string[] args)
    13         {
    14             Hashtable hashtable = new Hashtable();//实例化一个HashTable集合对象

              Student Zhang = new Student("张三", '男', 22);
              Student Li = new Student("李四", '女', 20); 
              Student Sun = new Student("小五", '女', 21);

    15             Console.WriteLine(" ***********使用HashTable集合***********");
    16             hashtable.Add("张三", Zhang);
    17             hashtable.Add("李四", Li);
    18             hashtable.Add("小五", Sun);
    19 Console.WriteLine("***********删除Student李四的信息***********"); 20 hashtable.Remove("李四");//用键(key)移除元素

    21 Console.WriteLine(" ***********使用Foreach循环遍历集合的所有值一***********"); 22 foreach (Student item in hashtable.Values)//循环遍历集合的值 23 { 24 Console.WriteLine("我的姓名是{0},年龄是{1},性别是{2}", item.Name, item.Age, item.Sex); 25 }
    26 Console.WriteLine(" ***********使用Foreach循环遍历集合的所有值二***********"); 27 foreach (Student item in hashtable.Values)//循环遍历集合的值 28 { 29 Student stu = (Student)item; 30 Console.WriteLine("我的姓名是{0},年龄是{1},性别是{2}", stu.Name, stu.Age, stu.Sex); 31 }
    32 Console.WriteLine(" ***********使用Foreach循环遍历集合的所有值三***********"); 33 foreach (var item in hashtable.Values)//循环遍历集合的值 34 { 35 Student stu = (Student)item; 36 Console.WriteLine("我的姓名是{0},年龄是{1},性别是{2}", stu.Name, stu.Age, stu.Sex); 37 } 38 39 Console.WriteLine("***********使用Foreach循环遍历Name值******"); 40 foreach (string Name in hashtable.Keys)//循环遍历集合的键(key) 41 { 42 Console.WriteLine("我的姓名是{0}", Name); 43 } 44 } 45 } 46 }

    3. 泛型集合: List<T>

    泛型是C#2.0中的一个新特性。泛型引入了一个新概念:类型参数。通过使用类型参数(T),减少了运行时强制转换成装箱操作的风险。通过泛型集合可以最大限度的重用代码、保护类型的安全及提高性能。

    不用点:List<T>对所保存元素做类型约束,而ArrayList可以增加任意类型。添加、读取值类型元素 List<T>无需拆箱装箱,而ArrayList需要做拆箱、装箱处理。

    相同点:通过索引访问集合中的元素,添加、删除元素方法相同

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Collections;
     6 using System.Diagnostics;
     7 
     8 namespace Demo
     9 {
    10     class Program
    11     {
    12         static void Main(string[] args)
    13         {
    14             List<Student> list = new List<Student>();//实例化一个List<>集合对象
    15             
    16             Student Zhang = new Student("张三", '', 22);
    17             Student Li = new Student("李四", '', 20);  
    18             Student Sun = new Student("小五", '', 21);
    19 
    20             Console.WriteLine("***********使用List<>集合***********");
    21             list.Add(Zhang);
    22             list.Add(Li);  
    23             list.Add(Sun);
    24 
    25             Console.WriteLine("***********使用Foreach循环遍历***********");
    26             list.Remove(Sun);//移除集合元素Sun的信息
    27             foreach (var item in list)
    28             {
    29                 Console.WriteLine("我的姓名是{0},年龄是{1},性别是{2}", item.Name, item.Age, item.Sex);
    30             }
    31   
    32             list.RemoveAt(0);//移除集合中索引为0的元素
    33             Console.WriteLine("***********使用For循环遍历*********");
    34             for (int i = 0; i < list.Count; i++)
    35             {
    36                 Console.WriteLine("我的姓名是{0},年龄是{1},性别是{2}", list[i].Name, list[i].Age, list[i].Sex);
    37             }
    38         }
    39     }
    40 }

    4.泛型集合Dictionary<K,V>

    它具有泛型的全部特性,编译时检查类型约束,获取元素时无需类型转换,并且它存储数据的方式和HashTable类似。也是通过Key/Value对元素保存的。

    不同点: Dictionary<K,V>对所保存的元素做类型约束,而HashTable可以增加任何类型。 Dictionary<K,V>添加、读取值类型元素无需拆箱、装箱,而HashTable需要做拆箱、装箱处理

    相同点:通过Key获取Value, 添加、删除、遍历元素方法相同

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Collections;
     6 using System.Diagnostics;
     7 
     8 namespace Demo
     9 {
    10     class Program
    11     {
    12         static void Main(string[] args)
    13         {
    14             Dictionary<string, Student> dictionary = new Dictionary<string, Student>();//实例化一个Directory<>集合对象
    15             
    16             Student Zhang = new Student("张三", '', 22);
    17             Student Li = new Student("李四", '', 20);          
    18             Student Sun = new Student("小五", '', 21);
    19 
    20             Console.WriteLine("*************使用Directory<>集合*************");
    21             dictionary.Add("张三", Zhang);
    22             dictionary.Add("李四", Li);  
    23             dictionary.Add("小五", Sun);
    24 
    25             Console.WriteLine("*************使用Foreach循环遍历***********");
    26             foreach (var item in dictionary.Values)
    27             {
    28                 Console.WriteLine("我的姓名是{0},年龄是{1},性别是{2}", item.Name, item.Age, item.Sex);
    29             }
    30 
    31             dictionary.Remove("李四");//移除集合中李四的信息
    32 
    33             Console.WriteLine("************使用Foreach循环遍历Name值******");
    34             foreach (string Name in dictionary.Keys)
    35             {
    36                 Console.WriteLine("我的姓名是{0}", Name);
    37             }
    39         }
    40     }
    41 }    
     1 using System;
     2 using System.Collections.Generic;
     3 public class Example
     4 {
     5          public static void Main()
     6          {
     7                //一、创建泛型哈希表,然后加入元素
     8                Dictionary<string, string> oscar = new Dictionary<string, string>();
     9                oscar.Add("哈莉?贝瑞", "《死囚之舞》");
    10                oscar.Add("朱迪?丹奇", "《携手人生》");
    11                oscar.Add("尼科尔?基德曼", "《红磨坊》");
    12                oscar.Add("詹妮弗?康纳利", "《美丽心灵》");
    13                oscar.Add("蕾妮?齐维格", "《BJ单身日记》");
    14 
    15                //二、删除元素
    16                oscar.Remove("詹妮弗?康纳利");
    17 
    18                //三、假如不存在元素则加入元素
    19                if (!oscar.ContainsKey("茜茜?斯派克")) oscar.Add("茜茜?斯派克", "《不伦之恋》");
    20                
    21 
    22                //四、显然容量和元素个数
    23                Console.WriteLine("元素个数: {0}", oscar.Count);
    24 
    25                //五、遍历集合
    26                Console.WriteLine("74届奥斯卡最佳女主角及其电影:");
    27                foreach (KeyValuePair<string, string> kvp in oscar)
    28                {
    29                       Console.WriteLine("姓名:{0},电影:{1}", kvp.Key, kvp.Value);
    30                }
    31 
    32               //六、得到哈希表中键的集合
    33               Dictionary<string, string>.KeyCollection keyColl = oscar.Keys;
    34               //遍历键的集合
    35               Console.WriteLine("最佳女主角:");
    36               foreach (string s in keyColl)
    37               {
    38                    Console.WriteLine(s);
    39               }
    40 
    41               //七、得到哈希表值的集合
    42               Dictionary<string, string>.ValueCollection valueColl = oscar.Values;
    43               //遍历值的集合
    44               Console.WriteLine("最佳女主角电影:");
    45               foreach (string s in valueColl)
    46               {
    47                    Console.WriteLine(s);
    48               }
    49 
    50               //八、使用TryGetValue方法获取指定键对应的值
    51               string slove = string.Empty;
    52               if (oscar.TryGetValue("朱迪?丹奇", out slove))
    53                      Console.WriteLine("我最喜欢朱迪?丹奇的电影{0}", slove);
    54               else
    55                      Console.WriteLine("没找到朱迪?丹奇的电影");
    56 
    57               //九、清空哈希表
    58               oscar.Clear();
    59               Console.ReadLine();
    60        }
    61 }

    5. 数组

    1. 定义

    首先,给定类型的数组只能保存该类型的元素。其次要规定数组的维数,可以用几何的知识理解数组的维数,可以用一维坐标轴来理解一维数组;用平面直角坐标系来理解二维数组;用三维立体坐标系来理解三维数组等。再次,数组必须规定每个维数的大小。

    int[] anIntArray;    //定义数组

    anIntArray={1,2,3};//上述代码定义了一个数组并对其进行了初始化。可以一次完成

    int [] sz=newint[];   //初始化数组,如果不指定大小,报语法错误。

    anIntArray=new  int[]{1,2,3};//用new关键字初始化数组的元素

    anIntArray=new  int[3]{1,2,3};//指定数组大小用new关键字初始化数组元素

    数组还有另外一种初始化方式,即指定数组大小并用new关键字初始化数组的元素:

    int[] anIntArray=new  int[3];

    执行时.NET将会为数组中的每一个元素赋予同一个(定义类型的)默认值。对于Int型的变量来说这个默认值是0。

    2、数组的索引

    数组的索引也就是通常所说的数组下标,英文为Index。数组的索引是从0开始的。

    3、数组的遍历

    C#提供了foreach语句以实现数组的遍历功能。可以使用foreach语句访问数组中的每个元素而不需要确切地知道每个元素的索引。

    int[] myArray=new int[5]  {14, 25, 7,36, 53};

    //采用foreach语句对myArray进行遍历

    foreach(int number inmyArray)

    Console.Write(number);

    for (int i= 0; i < 5; i++)

    Console.Write(myArray[i]);

  • 相关阅读:
    jquery ajax 赋值问题, 后面程序判断逻辑用
    jquery formValidator 表单验证插件, ajax无法传值到后台问题的解决
    学习写了一个点击按钮倒计时的jquery小插件
    点击按钮复制指定代码
    discuz 修改积分策略( 在周期中添加"每周" )
    php获取本周周一、周日时间,上周周一、周日时间,本月第一天,本月最后一天,上个月第一天,最后一天时间
    php用正则判断是否为数字
    discuz 标签详解
    用dwz时, 由于粗心产生的一些问题(记录方便自己查阅)
    yii mailer 扩展发送邮件
  • 原文地址:https://www.cnblogs.com/zhao987/p/12627083.html
Copyright © 2011-2022 走看看