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

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

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

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

数组有三种样式:

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

一维数组【Rank为1】:

T[] array;

锯齿数组【Rank为1】:

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

T[][] array;

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

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

T[,] array;

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

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

using System.Runtime.InteropServices;

namespace System.Collections
{
    [ComVisible(true)]
    [Guid("496B0ABE-CDEE-11d3-88E8-00902754C43A")]
    public interface IEnumerable
    {
        [DispId(-4)]
        IEnumerator GetEnumerator();
    }
}

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

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

部分数组ArraySegment<T>

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

ArraySegment<T>

public struct ArraySegment<T> : IList<T>, ICollection<T>, IEnumerable<T>, IEnumerable, IReadOnlyList<T>, IReadOnlyCollection<T>

常见的集合统计:

列表:List<T>

列表:List<T> 继承于 IList<T>继承于ICollection<T>继承于IEnumerable<T>继承于IEnumerable

链表:LinkedList<T>

链表:LinkedList<T>继承于ICollection<T>继承于IEnumerable<T>继承于IEnumerable

字典:Dictionary<TKey, TValue>

字典:Dictionary<TKey, TValue>继承于IDictionary<TKey, TValue>继承于ICollection<KeyValuePair<TKey, TValue>>继承于IEnumerable<T>继承于IEnumerable

队列:Queue<T>

队列:Queue<T>继承于IEnumerable<T>继承于IEnumerable

栈:Stack<T>

栈:Stack<T>继承于IEnumerable<T>继承于IEnumerable

哈希集:HashSet<T>

哈希集:HashSet<T>继承于ISet<T>继承于ICollection<T>继承于IEnumerable<T>继承于IEnumerable

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

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

测试程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CollectionAndArrayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            TestArray();
            TestSegmentArray();
            TestCollections();
            Console.ReadLine();
        }

        /// <summary>
        /// 一维数据、锯齿数组、二维数组
        /// </summary>
        static void TestArray() 
        {
            int[] oneDimensionalArray = new int[3] { 6, 9, 7 };
            Console.WriteLine(string.Join(",", oneDimensionalArray));
            Console.WriteLine($"一维数组的维数(秩)为【{oneDimensionalArray.Rank}】");
            int[][] sawtoothArray = new int[3][];
            sawtoothArray[0] = new int[2] { 1, 2 };
            sawtoothArray[1] = new int[3] { 1, 2, 3 };
            sawtoothArray[2] = new int[1] { 1 };
            Console.WriteLine(string.Join(".\n", sawtoothArray.Select(arr => string.Join(",", arr))));
            Console.WriteLine($"锯齿数组的维数(秩)为【{sawtoothArray.Rank}】");
            int[,] twoDimensionalArray = new int[3, 4] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
            for (int i = 0; i <= twoDimensionalArray.GetUpperBound(0); i++)
            {
                for (int j = 0; j <= twoDimensionalArray.GetUpperBound(1); j++)
                {
                    Console.Write($"{twoDimensionalArray[i, j]},");
                }
                Console.WriteLine();
            }
            Console.WriteLine($"二维数组的维数(秩)为【{twoDimensionalArray.Rank}】");
            int[,][] vs1 = new int[2, 3][];//定义一个二维数组,元素的每一个元素都是一个数组
            vs1[0, 0] = new int[3] { 4, 5, 6 };
            Console.WriteLine($"定义一个二维数组int[,][],元素的每一个元素都是一个数组.秩为【{vs1.Rank}】");
            int[][,] vs2 = new int[3][,];//定义一个一维数组,元素的每一个元素都是一个二维数组
            vs2[0] = new int[2, 3];
            Console.WriteLine($"定义一个一维数组int[][,],元素的每一个元素都是一个二维数组.秩为【{vs2.Rank}】");
        }

        /// <summary>
        /// 部分数组
        /// </summary>
        static void TestSegmentArray() 
        {
            int[] srcArray = new int[6] { 6, 5, 4, 3, 2, 1 };
            ArraySegment<int> segmentArray = new ArraySegment<int>(srcArray, 2, 3);
            Console.WriteLine($"部分数组的元素个数【{segmentArray.Count}】");
            Console.WriteLine(string.Join(",", segmentArray));
        }

        /// <summary>
        /// 测试集合:列表、链表、字典、队列、栈
        /// </summary>
        static void TestCollections() 
        {
            List<int> list = new List<int>() { 3, 6, 9 };
            Console.WriteLine($"打印列表:{string.Join(",", list)}");
            LinkedList<int> linkedList = new LinkedList<int>();
            LinkedListNode<int> node = new LinkedListNode<int>(5);
            linkedList.AddFirst(node);
            linkedList.AddAfter(node, 8);
            Console.WriteLine($"打印链表:{string.Join(",", linkedList)}");
            Dictionary<string, int> dict = new Dictionary<string, int>() { { "月清疏", 18 }, { "修吾", 3000 } };
            dict.Add("桑游", 19);
            dict["白茉晴"] = 16;
            Console.WriteLine($"打印字典:{string.Join(",", dict)}");

            //哈希集 元素是唯一的
            HashSet<int> hashSet = new HashSet<int>() { 20, 40 };
            hashSet.Add(20);
            hashSet.Add(30);
            Console.WriteLine($"打印哈希集:{string.Join(",", hashSet)}");

            //队列为先进先出
            Queue<int> queue = new Queue<int>(new int[] { 20, 35 });
            queue.Enqueue(29);
            int dequeueElement = queue.Dequeue();
            Console.WriteLine($"队列出列元素值:【{dequeueElement}】");
            Console.WriteLine($"打印队列:{string.Join(",", queue)}");

            //栈为后进先出
            Stack<int> stack = new Stack<int>(new int[] { 20, 35 });
            stack.Push(29);
            int popElement = stack.Pop();
            Console.WriteLine($"出栈元素值:【{popElement}】");
            Console.WriteLine($"打印栈:{string.Join(",", stack)}");
        }
    }
}

程序运行如图:

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

斯内科

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值