🧠 一、數組(Array)
1. 定義和初始化數組
// 定義并初始化數組
int[] numbers = new int[5]; // 默認值為 0// 聲明并賦值
string[] names = { "Tom", "Jerry", "Bob" };// 使用 new 初始化
double[] scores = new double[] { 90.5, 85.3, 92.7 };
2. 訪問和修改元素
Console.WriteLine(names[0]); // 輸出 Tom
names[1] = "Alice";
Console.WriteLine(names[1]); // 輸出 Alice
3. 數組切片(C# 8.0+)
int[] arr = { 1, 2, 3, 4, 5 };
int[] slice = arr[1..4]; // [2, 3, 4]
4. 遍歷數組
foreach (string name in names)
{Console.WriteLine(name);
}
5. 數組尺寸(Length)和 Rank
Console.WriteLine($"長度: {names.Length}");
Console.WriteLine($"維度數: {names.Rank}"); // 通常為 1
6. 多維數組(矩形數組)
二維數組:
int[,] matrix = {{1, 2},{3, 4}
};Console.WriteLine(matrix[0, 1]); // 輸出 2
三維數組:
int[,,] cube = new int[2, 2, 2];
cube[0, 0, 0] = 1;
7. 鋸齒數組(Jagged Array)——數組的數組
int[][] jagged = new int[][]
{new int[] { 1, 2 },new int[] { 3 },new int[] { 4, 5, 6 }
};Console.WriteLine(jagged[2][1]); // 輸出 5
8. 常用數組方法(System.Array)
Array.Sort(scores); // 排序
Array.Reverse(scores); // 反轉
int index = Array.IndexOf(names, "Alice"); // 查找索引
📦 二、集合(Collections)
1. ArrayList
(非泛型)
?不推薦使用,因為沒有類型安全。
ArrayList list = new ArrayList();
list.Add("Apple");
list.Add(100);
list.Remove("Apple");
2. List<T>
(泛型集合,推薦使用)
List<string> fruits = new List<string>() { "Apple", "Banana" };
fruits.Add("Orange");
fruits.Remove("Banana");foreach (string fruit in fruits)
{Console.WriteLine(fruit);
}
3. 集合初始值設定項(Collection Initializers)
List<int> numbersList = new List<int> { 1, 2, 3 };
4. SortedList<TKey, TValue>
(按鍵排序)
SortedList<int, string> sortedList = new SortedList<int, string>
{{ 3, "Three" },{ 1, "One" },{ 2, "Two" }
};foreach (var item in sortedList)
{Console.WriteLine($"{item.Key}: {item.Value}");
}
5. LinkedList<T>
(鏈表結構)
LinkedList<string> linkedList = new LinkedList<string>();
linkedList.AddLast("A");
linkedList.AddLast("B");
linkedList.AddFirst("X");
6. Dictionary<TKey, TValue>
(鍵值對)
Dictionary<string, int> ages = new Dictionary<string, int>
{{ "Tom", 25 },{ "Jerry", 30 }
};ages["Alice"] = 28;if (ages.ContainsKey("Tom"))
{Console.WriteLine(ages["Tom"]);
}
7. Queue<T>
(先進先出)
Queue<string> queue = new Queue<string>();
queue.Enqueue("Task 1");
queue.Enqueue("Task 2");Console.WriteLine(queue.Dequeue()); // Task 1
8. Stack<T>
(后進先出)
Stack<int> stack = new Stack<int>();
stack.Push(1);
stack.Push(2);Console.WriteLine(stack.Pop()); // 2
🧩 三、綜合練習項目模板(Program.cs)
using System;
using System.Collections;
using System.Collections.Generic;class Program
{static void Main(){Console.WriteLine("=== C# 數組與集合綜合練習 ===\n");// 1. 數組操作int[] nums = { 5, 2, 8, 1 };Array.Sort(nums);Console.WriteLine("排序后的數組:");foreach (int n in nums) Console.Write(n + " ");// 2. 多維數組int[,] matrix = { { 1, 2 }, { 3, 4 } };Console.WriteLine("\n\n矩陣元素:");for (int i = 0; i < matrix.GetLength(0); i++){for (int j = 0; j < matrix.GetLength(1); j++){Console.Write(matrix[i, j] + " ");}Console.WriteLine();}// 3. 列表 List<T>List<string> fruits = new List<string> { "Apple", "Banana" };fruits.Add("Orange");Console.WriteLine("\n水果列表:");foreach (string f in fruits) Console.WriteLine(f);// 4. 字典 DictionaryDictionary<string, int> ages = new Dictionary<string, int>{{ "Tom", 25 },{ "Jerry", 30 }};Console.WriteLine("\n年齡信息:");foreach (var item in ages){Console.WriteLine($"{item.Key}: {item.Value}");}// 5. 隊列 QueueQueue<string> queue = new Queue<string>();queue.Enqueue("任務1");queue.Enqueue("任務2");Console.WriteLine("\n隊列取出:" + queue.Dequeue());// 6. 棧 StackStack<int> stack = new Stack<int>();stack.Push(10);stack.Push(20);Console.WriteLine("棧取出:" + stack.Pop());Console.WriteLine("\n按任意鍵退出...");Console.ReadKey();}
}
📋 四、運行效果(模擬)
=== C# 數組與集合綜合練習 ===排序后的數組:
1 2 5 8 矩陣元素:
1 2
3 4 水果列表:
Apple
Banana
Orange年齡信息:
Tom: 25
Jerry: 30隊列取出:任務1
棧取出:20按任意鍵退出...
📌 五、總結對比表
類型 | 是否泛型 | 是否有序 | 是否可變 | 典型用途 |
---|---|---|---|---|
Array | ? | ? | ? | 固定大小數據存儲 |
List<T> | ? | ? | ? | 動態數組 |
Dictionary<TKey, TValue> | ? | ? | ? | 鍵值查找 |
SortedList<TKey, TValue> | ? | ? | ? | 按鍵排序 |
Queue<T> | ? | ? | ? | FIFO 場景 |
Stack<T> | ? | ? | ? | LIFO 場景 |
LinkedList<T> | ? | ? | ? | 插入/刪除頻繁 |