• C#中数组与列表,集合等的联系


    C#中,所有数组都自动继承于System.Array这个抽象类,数组都为引用类型,

    所有对数组的更新都会导致源数组的元素值的篡改。

    而所有集合的根都来自可枚举接口IEnumerable

    数组有三种样式:

    数组的Rank(秩)属性代表数组的维数

    一维数组【Rank为1】:

    T[] array;

    锯齿数组【Rank为1】:

    锯齿数组本质仍属于一维数组,只不过数组的某一个元素都是数组:

    T[][] array;

    多维数组【Rank为2~N,秩为逗号个数+1】:

    二维,三维等多维数组,使用[,,..,]标识

    T[,] array;

    数组实现了IList,ICollection等接口,因此数组是一种特殊的集合

    所有集合的根都来自可枚举接口IEnumerable

    1. using System.Runtime.InteropServices;
    2. namespace System.Collections
    3. {
    4. [ComVisible(true)]
    5. [Guid("496B0ABE-CDEE-11d3-88E8-00902754C43A")]
    6. public interface IEnumerable
    7. {
    8. [DispId(-4)]
    9. IEnumerator GetEnumerator();
    10. }
    11. }

    【数组一定是集合,集合不一定是数组】

    public abstract class Array : ICloneable, IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable

    部分数组ArraySegment

    一种结构,指代数组的一部分

    ArraySegment

    public struct ArraySegment : IList, ICollection, IEnumerable, IEnumerable, IReadOnlyList, IReadOnlyCollection

    常见的集合统计:

    列表:List

    列表:List 继承于 IList继承于ICollection继承于IEnumerable继承于IEnumerable

    链表:LinkedList

    链表:LinkedList继承于ICollection继承于IEnumerable继承于IEnumerable

    字典:Dictionary

    字典:Dictionary继承于IDictionary继承于ICollection>继承于IEnumerable继承于IEnumerable

    队列:Queue

    队列:Queue继承于IEnumerable继承于IEnumerable

    栈:Stack

    栈:Stack继承于IEnumerable继承于IEnumerable

    哈希集:HashSet

    哈希集:HashSet继承于ISet继承于ICollection继承于IEnumerable继承于IEnumerable

    计算机中的集合与数学中的集合:

    之前我们数学中定义的集合 集合中元素的三个特性:1、确定性、2、互异性、3、无序性。
    在编程语言中,只有HashSet继承于ISet 满足,其他都不是数学中的集合,计算机中的集合是广义的概念,包含数组、队列、字典、列表等

    测试程序:

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. namespace CollectionAndArrayDemo
    7. {
    8. class Program
    9. {
    10. static void Main(string[] args)
    11. {
    12. TestArray();
    13. TestSegmentArray();
    14. TestCollections();
    15. Console.ReadLine();
    16. }
    17. ///
    18. /// 一维数据、锯齿数组、二维数组
    19. ///
    20. static void TestArray()
    21. {
    22. int[] oneDimensionalArray = new int[3] { 6, 9, 7 };
    23. Console.WriteLine(string.Join(",", oneDimensionalArray));
    24. Console.WriteLine($"一维数组的维数(秩)为【{oneDimensionalArray.Rank}】");
    25. int[][] sawtoothArray = new int[3][];
    26. sawtoothArray[0] = new int[2] { 1, 2 };
    27. sawtoothArray[1] = new int[3] { 1, 2, 3 };
    28. sawtoothArray[2] = new int[1] { 1 };
    29. Console.WriteLine(string.Join(".\n", sawtoothArray.Select(arr => string.Join(",", arr))));
    30. Console.WriteLine($"锯齿数组的维数(秩)为【{sawtoothArray.Rank}】");
    31. int[,] twoDimensionalArray = new int[3, 4] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
    32. for (int i = 0; i <= twoDimensionalArray.GetUpperBound(0); i++)
    33. {
    34. for (int j = 0; j <= twoDimensionalArray.GetUpperBound(1); j++)
    35. {
    36. Console.Write($"{twoDimensionalArray[i, j]},");
    37. }
    38. Console.WriteLine();
    39. }
    40. Console.WriteLine($"二维数组的维数(秩)为【{twoDimensionalArray.Rank}】");
    41. int[,][] vs1 = new int[2, 3][];//定义一个二维数组,元素的每一个元素都是一个数组
    42. vs1[0, 0] = new int[3] { 4, 5, 6 };
    43. Console.WriteLine($"定义一个二维数组int[,][],元素的每一个元素都是一个数组.秩为【{vs1.Rank}】");
    44. int[][,] vs2 = new int[3][,];//定义一个一维数组,元素的每一个元素都是一个二维数组
    45. vs2[0] = new int[2, 3];
    46. Console.WriteLine($"定义一个一维数组int[][,],元素的每一个元素都是一个二维数组.秩为【{vs2.Rank}】");
    47. }
    48. ///
    49. /// 部分数组
    50. ///
    51. static void TestSegmentArray()
    52. {
    53. int[] srcArray = new int[6] { 6, 5, 4, 3, 2, 1 };
    54. ArraySegment<int> segmentArray = new ArraySegment<int>(srcArray, 2, 3);
    55. Console.WriteLine($"部分数组的元素个数【{segmentArray.Count}】");
    56. Console.WriteLine(string.Join(",", segmentArray));
    57. }
    58. ///
    59. /// 测试集合:列表、链表、字典、队列、栈
    60. ///
    61. static void TestCollections()
    62. {
    63. List<int> list = new List<int>() { 3, 6, 9 };
    64. Console.WriteLine($"打印列表:{string.Join(",", list)}");
    65. LinkedList<int> linkedList = new LinkedList<int>();
    66. LinkedListNode<int> node = new LinkedListNode<int>(5);
    67. linkedList.AddFirst(node);
    68. linkedList.AddAfter(node, 8);
    69. Console.WriteLine($"打印链表:{string.Join(",", linkedList)}");
    70. Dictionary<string, int> dict = new Dictionary<string, int>() { { "月清疏", 18 }, { "修吾", 3000 } };
    71. dict.Add("桑游", 19);
    72. dict["白茉晴"] = 16;
    73. Console.WriteLine($"打印字典:{string.Join(",", dict)}");
    74. //哈希集 元素是唯一的
    75. HashSet<int> hashSet = new HashSet<int>() { 20, 40 };
    76. hashSet.Add(20);
    77. hashSet.Add(30);
    78. Console.WriteLine($"打印哈希集:{string.Join(",", hashSet)}");
    79. //队列为先进先出
    80. Queue<int> queue = new Queue<int>(new int[] { 20, 35 });
    81. queue.Enqueue(29);
    82. int dequeueElement = queue.Dequeue();
    83. Console.WriteLine($"队列出列元素值:【{dequeueElement}】");
    84. Console.WriteLine($"打印队列:{string.Join(",", queue)}");
    85. //栈为后进先出
    86. Stack<int> stack = new Stack<int>(new int[] { 20, 35 });
    87. stack.Push(29);
    88. int popElement = stack.Pop();
    89. Console.WriteLine($"出栈元素值:【{popElement}】");
    90. Console.WriteLine($"打印栈:{string.Join(",", stack)}");
    91. }
    92. }
    93. }

    程序运行如图:

  • 相关阅读:
    Centos7安装wps无法打开及字体缺失的问题解决
    windows系统利用powershell查看系统支持那些Windows功能选项
    .net core基于HttpClient实现的网络请求库
    企业移动设备管理(MDM)概述
    2020年大厂Java面试前复习的正确姿势(800+面试题答案解析)
    Arthas诊断工具获取类属性、实例属性值
    斜率优化DP day47
    [极客大挑战 2019]Knife 1(两种解法)
    Java 1.8引入StringJoiner,用与字符串拼接
    圈复杂度检测
  • 原文地址:https://blog.csdn.net/ylq1045/article/details/138678267