一、list集合基本使用
1.添加元素
① 單個元素添加
List<int> list = new List<int>();for (int i = 0; i < 3; i++){list.Add(i);}//輸出:0,1,2
②初始化時添加元素?
List<int> list2 = new List<int> { 1, 2, 3 };//輸出:0,1,2
③集合中添加集合?
List<int> list2 = new List<int> { 1, 2, 3 };List<int> list3 = new List<int>();list3.AddRange(list2);//輸出:0,1,2
2.刪除元素
①刪除集合中匹配項元素,未找到匹配元素,刪除失敗,返回false
List<int> list = new List<int> { 1, 5, 7, 2, 8 };bool b= list.Remove(3); // 刪除元素 3Console.WriteLine(b); //false 刪除失敗
List<int> list = new List<int> { 1, 2, 2, 2, 3 };bool b = list.Remove(3); // 刪除元素 3Console.WriteLine(b); //true 刪除成功 輸出:1,2,2,2
②刪除集合中指定索引元素,索引從零開始
List<int> list = new List<int> { 1, 2, 3, 4, 5 };list.RemoveAt(2); // 刪除索引為 2 的元素,即元素 3 輸出結果:1,2,4,5
③刪除集合中一定范圍內的元素,索引從零開始
List<int> list = new List<int> { 1, 2, 3, 4, 5 };list.RemoveRange(0, 2); //刪除一定范圍內元素,從0開始,刪除2個 輸出結果:3,4,5
④刪除集合中符合條件的數據
List<int> list = new List<int> { 1, 2, 3, 4, 5 };list.RemoveAll(i => i % 2 == 0); // 刪除所有偶數元素,輸出結果1,3,5
3.改變集合中的元素? (不重要,不常用)
4.查找集合中的元素
①查找集合中匹配項元素,未找到匹配元素,刪除-1,找到返回從0開始的索引
List<int> list = new List<int>() { 1, 2, 3, 4, 5 };int index = list.IndexOf(5); // 返回4 未找到返回-1
②根據指定的條件查找符合條件的所有元素,并返回一個新的List<T>集合
List<int> list = new List<int>() { 1, 2, 3, 4, 5 };List<int> newList = list.FindAll(x => x > 3); // 返回{ 4, 5 }
③使用Contains方法判斷元素是否存在
List<int> list = new List<int>() { 1, 2, 3, 4, 5 };bool contains1 = list.Contains(3); // 返回truebool contains2 = list.Contains(6); // 返回false
二、list集合進階使用
1、排序
①升序方法1
List<int> list = new List<int>() { 1, 3, 6, 2, 9, 7 };list.Sort(); //對集合內元素進行升序排列 輸出:1,2,3,6,7,9
①升序方法2
List<int> list = new List<int>() { 1, 3, 6, 2, 9, 7 };list.Sort((a, b) => a.CompareTo(b)); //對集合內元素進行升序排列 輸出:1,2,3,6,7,9
②降序方法1
List<int> list = new List<int>() { 1, 3, 6, 2, 9, 7 };list.Sort(); //先升序list.Reverse(); //翻轉元素,達到降序的目的 輸出:9,7,6,3,2,1
②降序方法2
List<int> list = new List<int>() { 1, 3, 6, 2, 9, 7 };list.Sort((x, y) => y.CompareTo(x)); // 降序 輸出:9,7,6,3,2,1
2、求集合內全部元素平局值
List<int> list = new List<int>() { 1, 3, 6, 2, 9, 7 };double d= list.Average(); //計算集合內全部數據的平均值
3、求集合內全部元素之和
List<int> list = new List<int>() { 1, 3, 6, 2, 9, 7 };int i= list.Sum(); //計算集合內所有元素之和 輸出:28
4、求集合內元素最大值
List<int> list = new List<int>() { 1, 3, 6, 2, 9, 7 };int i= list.Max(); //計算集合內所有元素中最大值 輸出:9
5、求集合內元素最小值
List<int> list = new List<int>() { 1, 3, 6, 2, 9, 7 };int i = list.Min(); //計算集合內所有元素中最大值 輸出:1
6、集合轉數組
List<int> list = new List<int>() { 1, 3, 6, 2, 9, 7 };int[] ints = list.ToArray(); //list集合轉為int類型數組
7、集合內元素去重
List<int> list = new List<int>() { 7, 3, 6, 3, 6, 7 };list= list.Distinct().ToList(); //list集合內元素去重 輸出:7,3,6
8、復制集合中指定索引長度元素至新集合中?
List<int> list = new List<int>() { 1, 3, 6, 2, 8, 7 };list= list.GetRange(0, list.Count-3); //從集合指定索引處,拷貝指定長度數量 存儲在新集合中 輸出:1,3,6
9、連接兩個集合中全部元素生成新集合
List<int> list1 = new List<int> { 1, 2, 3 };List<int> list2 = new List<int> { 4, 5, 6 };IEnumerable<int> combinedList = list1.Concat(list2); //連接兩個集合 ,生成IEnumerable類型int[] ints= combinedList.ToArray(); //因IEnumerable類型使用foreach遍歷方便,為了使用for循環變量轉為數組類型for (int i = 0; i < ints.Length; i++){Console.WriteLine(ints[i]); //輸出連接后的集合元素 輸出結果:1,2,3,4,5,6}