c#学习笔记05——数组&集合

  • 数组
    • 声明数组
       1 1.一维数组的定义:
       2 数据类型[] 数组名=new 数据类型[大小];
       3 eg:
       4 int[] num=new int[5];
       5 int[] num={4,2,3,4,5};
       6 string[] str=new string[6];
       7 2.多维数组的定义
       8 int[,] num=new int[3,4];//定义二维数组
       9 int[,,] num=new int[3,4,5];//定义三维数组
      10 多维数组可以理解为每个元素自身是一个数组的一维数组,这种情况下数组又称为交错数组,交错数组的声明:
      11 int[][] jaggedArray=new int[3][];
    • 初始化数组
       1 //一位数值数组
       2 int[] n1=new int[4] {2,4,6,8};
       3 int[] n2=new int[]{2,4,6,8};
       4 int[] n3={2,4,6,8};
       5 //一维字符数组
       6 string[] s1=new string[3]{"john","Paul","Mary"};
       7 string[] s2=new string[]{"john","Paul","Mary"};
       8 string[] s3={"john","Paul","Mary"};
       9 
      10 //多维数组
      11 int[,] n4=new int[3,2] {{12},{34},{56}};
      12 int[,] n5=new int[,] {{12},{34},{56}};
      13 int[,] n6={{12},{34},{56}};
      14 
      15 //交错数组
      16 int[][] n7=new int[2][]{new int[]{2,4,6},new int[]{1,3,5,7,9}};
      17 int[][] n8=new int[][]{new int[]{2,4,6},new int[]{1,3,5,7,9}};
      18 int[][]n9={new int[]{2,4,6},new int[]{1,3,5,7,9}};
    • 访问单独的数组元素:数组可以通过索引来访问其中的数据,数组的索引是从0开始,也就是第一个元素对应的索引值为0,后面的逐个递增,访问的方式如下:数组名[索引值];
       1 //一维数值数组
       2 int[] n1=new int[4]{2,4,6,8};
       3 int a=n1[0];//第一个元素
       4 
       5 //多维数组
       6 int[,] n4=int new[3,2] {{1,2},{3,4},{5,6}};
       7 int e=n4[0,0];//第1行1列元素
       8 
       9 //交错数组
      10 int[][] n7=new int[2][]{new int[]{2,4,6},new int[]{1,3,5,7,9}};
      11 int g=n7[1][2];//第2行3列元素
    • 遍历数组
       11)可以使用foreach语句遍历数组
       2 //一维数值数组
       3 int[] n1=new int[4]{2,4,6,8};
       4 foreach(int i in n1)
       5 {
       6       System.Console.Write("{0}",i);
       7 }
       8 //二维数组
       9 int[,]n4=new int[3,2]{{1,2},{3,4},{5,6}};
      10 foreach(int i in n4)
      11 {
      12       System.Console.Write("{0}",i);
      13 }
      14 Console.WriteLine(" ");
      152)使用for循环遍历数组
      16 //一维数值数组
      17 int[] n1=new int[4]{2,4,6,8};
      18 for(int i=0;i<n1.Length;i++)
      19 {
      20         Console.WriteLine(n1[i]);
      21 }
      22 //二维数组
      23 for(int i=0;i<n4.GetLength(0);i++)//遍历行
      24 {
      25       for(int j=0;j<n4.GetLength(1);j++)//遍历列
      26         {
      27              Console.Write(n4[i,j]);
      28              Console.Write(" ");
      29          }
      30 }
      31 Console.WriteLine(" ");
      323)使用Ienumerator接口遍历数组
      33 //一位数值数组
      34 int[] n1=new int[4]{2,4,6,8};
      35 Ienumberator eed=n1.GetEnmerator();
      36 while(eed.MoveNext())
      37 {
      38         Console.WriteLine(eed.Current.ToString());
      39 }
      40 //二维数组
      41 Ienumberator eed=n4.GetEnumberator();
      42 while(eed.MoveNext())
      43 {
      44      Console.Write(eed.Current.ToString());
      45      Console.Write(" ");
      46 }
    • Array类:在C#中,声明的数组在后台会被创建上一个派生子抽象类Array的新类,这样数组就可以使用Array类为每个C#数组定义的方法和属性。
  • 集合:集合如同,用来存储和管理一组特定类型的数据结构对象,不过,与数组不同的是,集合除了基本的数据处理功能外,还提供了各种数据结构及算法的实现,如队列,链表,排序等。在C#中集合类是命名空间System.Collections或System.Collection.Generic的一部分,而且需要实现一个或多个Icollection、Icomparer、Ienumerable、Ilist、IdictionaryEnumerator接口以及它们的等效泛型接口。
    • 命名空间System.Collections中的集合可以分为如下3种:
      1. 常用集合:数据集合的常见变体,如哈希表,队列,堆栈,字典和列表。常用集合有泛型和非泛型之分。
        1. 有序集合
        2. 索引集合
        3. 键式集合
      2. 位集合:这些集合中的元素均为位标识。它们的行为与其他集合稍有不同。
      3. 专用集合:这些集合都具有专门的用途,通常用于处理特定的元素类型,如StringDictionary.
    • ArrayList集合类:用于实现可以动态的添加元素的数据链表,也就是其大小可以按需进行动态的增加。
    • Queue集合类:表队列
    • Stack集合类:表堆栈
    • Hashtable集合类:表示键/值(key/value)对的集合,这些键/值对根据键的哈希代码进行组织。Key区分大小写
原文地址:https://www.cnblogs.com/yuelien/p/6664541.html