2022-12-23 18:28:17 +08:00
|
|
|
|
/**
|
|
|
|
|
* File: list.cs
|
|
|
|
|
* Created Time: 2022-12-23
|
|
|
|
|
* Author: haptear (haptear@hotmail.com)
|
|
|
|
|
*/
|
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
namespace hello_algo.chapter_array_and_linkedlist;
|
|
|
|
|
|
2023-04-23 03:03:12 +08:00
|
|
|
|
public class list {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
[Test]
|
2023-04-23 03:03:12 +08:00
|
|
|
|
public void Test() {
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
/* 初始化列表 */
|
|
|
|
|
int[] numbers = new int[] { 1, 3, 2, 5, 4 };
|
2023-10-09 18:20:42 +08:00
|
|
|
|
List<int> nums = numbers.ToList();
|
|
|
|
|
Console.WriteLine("列表 nums = " + string.Join(",", nums));
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
/* 访问元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
int num = nums[1];
|
2023-02-08 22:18:02 +08:00
|
|
|
|
Console.WriteLine("访问索引 1 处的元素,得到 num = " + num);
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
/* 更新元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums[1] = 0;
|
|
|
|
|
Console.WriteLine("将索引 1 处的元素更新为 0 ,得到 nums = " + string.Join(",", nums));
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
/* 清空列表 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.Clear();
|
|
|
|
|
Console.WriteLine("清空列表后 nums = " + string.Join(",", nums));
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
/* 尾部添加元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.Add(1);
|
|
|
|
|
nums.Add(3);
|
|
|
|
|
nums.Add(2);
|
|
|
|
|
nums.Add(5);
|
|
|
|
|
nums.Add(4);
|
|
|
|
|
Console.WriteLine("添加元素后 nums = " + string.Join(",", nums));
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
/* 中间插入元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.Insert(3, 6);
|
|
|
|
|
Console.WriteLine("在索引 3 处插入数字 6 ,得到 nums = " + string.Join(",", nums));
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
/* 删除元素 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.RemoveAt(3);
|
|
|
|
|
Console.WriteLine("删除索引 3 处的元素,得到 nums = " + string.Join(",", nums));
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
/* 通过索引遍历列表 */
|
|
|
|
|
int count = 0;
|
2023-10-09 18:20:42 +08:00
|
|
|
|
for (int i = 0; i < nums.Count; i++) {
|
2023-11-14 21:27:35 +08:00
|
|
|
|
count += nums[i];
|
2023-02-08 22:18:02 +08:00
|
|
|
|
}
|
|
|
|
|
/* 直接遍历列表元素 */
|
|
|
|
|
count = 0;
|
2023-11-14 21:27:35 +08:00
|
|
|
|
foreach (int x in nums) {
|
|
|
|
|
count += x;
|
2023-02-08 22:18:02 +08:00
|
|
|
|
}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
/* 拼接两个列表 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
List<int> nums1 = new() { 6, 8, 7, 10, 9 };
|
|
|
|
|
nums.AddRange(nums1);
|
|
|
|
|
Console.WriteLine("将列表 nums1 拼接到 nums 之后,得到 nums = " + string.Join(",", nums));
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
/* 排序列表 */
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.Sort(); // 排序后,列表元素从小到大排列
|
|
|
|
|
Console.WriteLine("排序列表后 nums = " + string.Join(",", nums));
|
2022-12-23 15:42:02 +08:00
|
|
|
|
}
|
|
|
|
|
}
|