C# List的詳細用法
阿新 • • 發佈:2018-11-28
作者:沐雪
連結:
http://www.cnblogs.com/puzi0315/archive/2012/08/08/2628981.html
C# List<T>的詳細用法 所屬名稱空間:System.Collections.Generic publicclassList<T> : IList<T>,ICollection<T>, IEnumerable<T>, IList,ICollection, IEnumerable
List<T>類是 ArrayList 類的泛型等效類。該類使用大小可按需動態增加的陣列實現 IList<T> 泛型介面。 泛型的好處: 它為使用c#語言編寫面向物件程式增加了極大的效力和靈活性。不會強行對值型別進行裝箱和拆箱,或對引用型別進行向下強制型別轉換,所以效能得到提高。 效能注意事項:在決定使用IList<T> 還是使用ArrayList類(兩者具有類似的功能)時,記住IList<T> 類在大多數情況下執行得更好並且是型別安全的。如果對IList<T> 類的型別 T 使用引用型別,則兩個類的行為是完全相同的。但是,如果對型別 T 使用值型別,則需要考慮實現和裝箱問題。 用微軟的話講:“新增到ArrayList 中的任何引用或值型別都將隱式地向上強制轉換為Object。如果項是值型別,則必須在將其新增到列表中時進行裝箱操作,在檢索時進行取消裝箱操作。強制轉換以及裝箱和取消裝箱操作都會降低效能;在必須對大型集合進行迴圈訪問的情況下,裝箱和取消裝箱的影響非常明顯。” 一、List的基礎、常用方法:
宣告:
1、List<T> mList = newList<T>();
//T為列表中元素型別,現在以string型別作為例子
E.g.: List<string> mList= newList<string>();
2、 List<T>testList =new List<T> (IEnumerable<T>collection); //以一個集合作為引數建立List E.g.:string[] temArr = { "Ha","Hunter", "Tom","Lily", "Jay","Jim", "Kuku","Locu" }; List<string>testList = newList<string>(temArr);
新增元素:
1、List. Add(T item)
//新增一個元素
E.g.:mList.Add("John");
2、List. AddRange(IEnumerable<T>collection) //新增一組元素 E.g.: string[] temArr = { "Ha","Hunter", "Tom","Lily", "Jay","Jim", "Kuku", "Locu"}; mList.AddRange(temArr);
3、Insert(int index, T item); //在index位置新增一個元素 E.g.:mList.Insert(1, "Hei");
遍歷List中元素:
foreach (T element in mList)
//T的型別與mList宣告時一樣
{
Console.WriteLine(element);
}
E.g.:
foreach (string s in mList)
{
Console.WriteLine(s);
}
刪除元素: 1、List. Remove(T item) //刪除一個值 E.g.:mList.Remove("Hunter"); 2、List. RemoveAt(intindex); //刪除下標為index的元素 E.g.:mList.RemoveAt(0); 3、List. RemoveRange(intindex, int count); 從下標index開始,刪除count個元素 E.g.:mList.RemoveRange(3, 2);
判斷某個元素是否在該List中: List. Contains(T item) //返回true或false,很實用 E.g.: if (mList.Contains("Hunter")) { Console.WriteLine("There is Hunter in the list"); } else { mList.Add("Hunter"); Console.WriteLine("Add Hunter successfully."); } 給List裡面元素排序: List. Sort () //預設是元素第一個字母按升序 E.g.: mList.Sort();
給List裡面元素順序反轉: List. Reverse () //可以與List. Sort ()配合使用,達到想要的效果 E.g.:mList.Sort();
List清空: List. Clear () E.g.: mList.Clear();
獲得List中元素數目: List. Count () //返回int值 E.g.: int count = mList.Count(); Console.WriteLine("The num of elements in the list: "+count);
二、List的進階、強大方法: 舉例用的List: string[] temArr = { Ha","Hunter", "Tom","Lily", "Jay","Jim", "Kuku",""Locu"}; mList.AddRange(temArr);
List.Find 方法: //搜尋與指定謂詞所定義的條件相匹配的元素,並返回整個 List 中的第一個匹配元素。 public T Find(Predicate<T>match); Predicate是對方法的委託,如果傳遞給它的物件與委託中定義的條件匹配,則該方法返回 true。當前 List 的元素被逐個傳遞給Predicate委託,並在 List 中向前移動,從第一個元素開始,到最後一個元素結束。當找到匹配項時處理即停止。Predicate可以委託給一個函式或者一個拉姆達表示式委託給拉姆達表示式: E.g.: stringlistFind = mList.Find(name => { if(name.Length > 3){ returntrue; } returnfalse; } ); //name是變數,代表的是mList //中元素,自己設定
Console.WriteLine(listFind); //輸出是Hunter
委託給一個函式: E.g.: string listFind1 = mList.Find(ListFind); //委託給ListFind函式 Console.WriteLine(listFind); //輸出是Hunter ListFind函式: public bool ListFind(string name) { if(name.Length > 3) { returntrue; } returnfalse; } 這兩種方法的結果是一樣的。
List.FindLast 方法: //搜尋與指定謂詞所定義的條件相匹配的元素,並返回整個 List 中的最後一個匹配元素。 public T FindLast(Predicate<T>match); 用法與List.Find相同。
List.TrueForAll方法: 確定是否 List 中的每個元素都與指定的謂詞所定義的條件相匹配。 publicbool TrueForAll(Predicate<T> match); 委託給拉姆達表示式: E.g.: bool flag= mList.TrueForAll(name => { if(name.Length > 3) { returntrue; } else { returnfalse; } } ); Console.WriteLine("True for all: "+flag); //flag值為false 委託給一個函式,這裡用到上面的ListFind函式: E.g.:bool flag = mList.TrueForAll(ListFind); //委託給ListFind函式 Console.WriteLine("True forall: "+flag); //flag值為false 這兩種方法的結果是一樣的
List.FindAll方法:檢索與指定謂詞所定義的條件相匹配的所有元素。 publicList<T>FindAll(Predicate<T> match); E.g.: List<string> subList =mList.FindAll(ListFind); //委託給ListFind函式 foreach (string s in subList) { Console.WriteLine("element in subList: "+s); } 這時subList儲存的就是所有長度大於3的元素 List.Take(n): 獲得前n行 返回值為IEnumetable<T>,T的型別與List<T>的型別一樣 E.g.: IEnumerable<string>takeList= mList.Take(5); foreach(string s intakeList) { Console.WriteLine("element in takeList: " + s); } 這時takeList存放的元素就是mList中的前5個 List.Where方法:檢索與指定謂詞所定義的條件相匹配的所有元素。跟List.FindAll方法類似。 E.g.: IEnumerable<string> whereList = mList.Where(name => { if(name.Length > 3) { returntrue; } else { returnfalse; } }); foreach (string s in subList) { Console.WriteLine("element in subList: "+s); } 這時subList儲存的就是所有長度大於3的元素 List.RemoveAll方法:移除與指定的謂詞所定義的條件相匹配的所有元素。 publicint RemoveAll(Predicate<T> match); E.g.: mList.RemoveAll(name => { if(name.Length > 3) { returntrue; } else { returnfalse; } }); foreach (string s in mList) { Console.WriteLine("element in mList: " + s); } 這時mList儲存的就是移除長度大於3之後的元素。
C# List<T>的詳細用法 所屬名稱空間:System.Collections.Generic publicclassList<T> : IList<T>,ICollection<T>, IEnumerable<T>, IList,ICollection, IEnumerable
List<T>類是 ArrayList 類的泛型等效類。該類使用大小可按需動態增加的陣列實現 IList<T> 泛型介面。 泛型的好處: 它為使用c#語言編寫面向物件程式增加了極大的效力和靈活性。不會強行對值型別進行裝箱和拆箱,或對引用型別進行向下強制型別轉換,所以效能得到提高。 效能注意事項:在決定使用IList<T> 還是使用ArrayList類(兩者具有類似的功能)時,記住IList<T> 類在大多數情況下執行得更好並且是型別安全的。如果對IList<T> 類的型別 T 使用引用型別,則兩個類的行為是完全相同的。但是,如果對型別 T 使用值型別,則需要考慮實現和裝箱問題。 用微軟的話講:“新增到ArrayList 中的任何引用或值型別都將隱式地向上強制轉換為Object。如果項是值型別,則必須在將其新增到列表中時進行裝箱操作,在檢索時進行取消裝箱操作。強制轉換以及裝箱和取消裝箱操作都會降低效能;在必須對大型集合進行迴圈訪問的情況下,裝箱和取消裝箱的影響非常明顯。” 一、List的基礎、常用方法:
2、 List<T>testList =new List<T> (IEnumerable<T>collection); //以一個集合作為引數建立List E.g.:string[] temArr = { "Ha","Hunter", "Tom","Lily", "Jay","Jim", "Kuku","Locu" }; List<string>testList = newList<string>(temArr);
新增元素:
2、List. AddRange(IEnumerable<T>collection) //新增一組元素 E.g.: string[] temArr = { "Ha","Hunter", "Tom","Lily", "Jay","Jim", "Kuku", "Locu"}; mList.AddRange(temArr);
3、Insert(int index, T item); //在index位置新增一個元素 E.g.:mList.Insert(1, "Hei");
遍歷List中元素:
刪除元素: 1、List. Remove(T item) //刪除一個值 E.g.:mList.Remove("Hunter"); 2、List. RemoveAt(intindex); //刪除下標為index的元素 E.g.:mList.RemoveAt(0); 3、List. RemoveRange(intindex, int count); 從下標index開始,刪除count個元素 E.g.:mList.RemoveRange(3, 2);
判斷某個元素是否在該List中: List. Contains(T item) //返回true或false,很實用 E.g.: if (mList.Contains("Hunter")) { Console.WriteLine("There is Hunter in the list"); } else { mList.Add("Hunter"); Console.WriteLine("Add Hunter successfully."); } 給List裡面元素排序: List. Sort () //預設是元素第一個字母按升序 E.g.: mList.Sort();
給List裡面元素順序反轉: List. Reverse () //可以與List. Sort ()配合使用,達到想要的效果 E.g.:mList.Sort();
List清空: List. Clear () E.g.: mList.Clear();
獲得List中元素數目: List. Count () //返回int值 E.g.: int count = mList.Count(); Console.WriteLine("The num of elements in the list: "+count);
二、List的進階、強大方法: 舉例用的List: string[] temArr = { Ha","Hunter", "Tom","Lily", "Jay","Jim", "Kuku",""Locu"}; mList.AddRange(temArr);
List.Find 方法: //搜尋與指定謂詞所定義的條件相匹配的元素,並返回整個 List 中的第一個匹配元素。 public T Find(Predicate<T>match); Predicate是對方法的委託,如果傳遞給它的物件與委託中定義的條件匹配,則該方法返回 true。當前 List 的元素被逐個傳遞給Predicate委託,並在 List 中向前移動,從第一個元素開始,到最後一個元素結束。當找到匹配項時處理即停止。Predicate可以委託給一個函式或者一個拉姆達表示式委託給拉姆達表示式: E.g.: stringlistFind = mList.Find(name => { if(name.Length > 3){ returntrue; } returnfalse; } ); //name是變數,代表的是mList //中元素,自己設定
Console.WriteLine(listFind); //輸出是Hunter
委託給一個函式: E.g.: string listFind1 = mList.Find(ListFind); //委託給ListFind函式 Console.WriteLine(listFind); //輸出是Hunter ListFind函式: public bool ListFind(string name) { if(name.Length > 3) { returntrue; } returnfalse; } 這兩種方法的結果是一樣的。
List.FindLast 方法: //搜尋與指定謂詞所定義的條件相匹配的元素,並返回整個 List 中的最後一個匹配元素。 public T FindLast(Predicate<T>match); 用法與List.Find相同。
List.TrueForAll方法: 確定是否 List 中的每個元素都與指定的謂詞所定義的條件相匹配。 publicbool TrueForAll(Predicate<T> match); 委託給拉姆達表示式: E.g.: bool flag= mList.TrueForAll(name => { if(name.Length > 3) { returntrue; } else { returnfalse; } } ); Console.WriteLine("True for all: "+flag); //flag值為false 委託給一個函式,這裡用到上面的ListFind函式: E.g.:bool flag = mList.TrueForAll(ListFind); //委託給ListFind函式 Console.WriteLine("True forall: "+flag); //flag值為false 這兩種方法的結果是一樣的
List.FindAll方法:檢索與指定謂詞所定義的條件相匹配的所有元素。 publicList<T>FindAll(Predicate<T> match); E.g.: List<string> subList =mList.FindAll(ListFind); //委託給ListFind函式 foreach (string s in subList) { Console.WriteLine("element in subList: "+s); } 這時subList儲存的就是所有長度大於3的元素 List.Take(n): 獲得前n行 返回值為IEnumetable<T>,T的型別與List<T>的型別一樣 E.g.: IEnumerable<string>takeList= mList.Take(5); foreach(string s intakeList) { Console.WriteLine("element in takeList: " + s); } 這時takeList存放的元素就是mList中的前5個 List.Where方法:檢索與指定謂詞所定義的條件相匹配的所有元素。跟List.FindAll方法類似。 E.g.: IEnumerable<string> whereList = mList.Where(name => { if(name.Length > 3) { returntrue; } else { returnfalse; } }); foreach (string s in subList) { Console.WriteLine("element in subList: "+s); } 這時subList儲存的就是所有長度大於3的元素 List.RemoveAll方法:移除與指定的謂詞所定義的條件相匹配的所有元素。 publicint RemoveAll(Predicate<T> match); E.g.: mList.RemoveAll(name => { if(name.Length > 3) { returntrue; } else { returnfalse; } }); foreach (string s in mList) { Console.WriteLine("element in mList: " + s); } 這時mList儲存的就是移除長度大於3之後的元素。