C#中,所有数组都自动继承于System.Array这个抽象类,数组都为引用类型,
所有对数组的更新都会导致源数组的元素值的篡改。
而所有集合的根都来自可枚举接口IEnumerable
数组的Rank(秩)属性代表数组的维数
T[] array;
锯齿数组本质仍属于一维数组,只不过数组的某一个元素都是数组:
T[][] array;
二维,三维等多维数组,使用[,,..,]标识
T[,] array;
- 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
public struct ArraySegment : IList, ICollection, IEnumerable, IEnumerable, IReadOnlyList, IReadOnlyCollection
列表:List
链表:LinkedList
字典:Dictionary
队列:Queue
栈:Stack
哈希集:HashSet
- 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();
- }
-
- ///
- /// 一维数据、锯齿数组、二维数组
- ///
- 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}】");
- }
-
- ///
- /// 部分数组
- ///
- 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));
- }
-
- ///
- /// 测试集合:列表、链表、字典、队列、栈
- ///
- 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)}");
- }
- }
- }
