網(wǎng)上的List泛型用法,未驗證,目測基本正確,教學(xué)用資料。
1、? List的基礎(chǔ)、常用方法:
聲明:?
1、List<T> mList = new List<T>();??
T為列表中元素類型,現(xiàn)在以string類型作為例子
E.g.: List<string> mList = new List<string>();
2、List<T> testList =new List<T>?(IEnumerable<T> collection);
?????以一個集合作為參數(shù)創(chuàng)建List
???? E.g.:
string[] temArr =?{?"Ha",?"Hunter",?"Tom",?"Lily",?"Jay",?"Jim",?"Kuku",?"Locu"?};
List<string> testList = new List<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(int index);???刪除下標(biāo)為index的元素
E.g.:?? mList.RemoveAt(0);
? 3、 List. RemoveRange(int index, int count);
從下標(biāo)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 ()???默認(rèn)是元素第一個字母按升序
E.g.:?? mList.Sort();
給List里面元素順序反轉(zhuǎn):
List. Reverse ()???可以與List. Sort ()配合使用,達到想要的效果
E.g.:?? mList.Sort();
List清空:List. Clear ()?
E.g.:?? mList.Clear();
??獲得List中元素數(shù)目:
List. Count ()????返回int值(轉(zhuǎn)者注:實測不帶“()”也可)
E.g.:
int count = mList.Count();
?????? Console.WriteLine("The num of elements in the list:?"?+count);
?(轉(zhuǎn)者注:學(xué)生基礎(chǔ)學(xué)習(xí)到此為止?。?/p>
2、? 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。當(dāng)前 List 的元素被逐個傳遞給Predicate委托,并在 List 中向前移動,從第一個元素開始,到最后一個元素結(jié)束。當(dāng)找到匹配項時處理即停止。
Predicate?可以委托給一個函數(shù)或者一個拉姆達表達式:
委托給拉姆達表達式:
E.g.:
????? string listFind = mList.Find(name =>??//name是變量,代表的是mList
??????{??????????????????????????????//中元素,自己設(shè)定
????????? if (name.Length?> 3)
??????????{
????????????? return true;
??????????}
????????????? return false;
???????});
?????? Console.WriteLine(listFind);?????//輸出是Hunter
委托給一個函數(shù):
E.g.:
string listFind1 = mList.Find(ListFind);??//委托給ListFind函數(shù)
Console.WriteLine(listFind);???????????//輸出是Hunter
ListFind函數(shù):?
public bool ListFind(string name)
????????{
??????????? if (name.Length?> 3)
????????????{
??????????????? return true;
????????????}
??????????? return false;
????????}
這兩種方法的結(jié)果是一樣的。
List.FindLast 方法:搜索與指定謂詞所定義的條件相匹配的元素,并返回整個 List 中的最后一個匹配元素。?
public T FindLast(Predicate<T> match);
用法與List.Find相同。
List.TrueForAll方法:??確定是否List中的每個元素都與指定的謂詞所定義的條件相匹配。
public bool TrueForAll(Predicate<T> match);
委托給拉姆達表達式:
E.g.:
??????????? bool flag = mList.TrueForAll(name =>
????????????{
??????????????? if (name.Length?> 3)
????????????????{
??????????????????? return true;
????????????????}
??????????????? else
????????????????{
??????????????????? return false;
????????????????}
????????????}
????????????);
?? Console.WriteLine("True for all:??"+flag);??//flag值為false
委托給一個函數(shù),這里用到上面的ListFind函數(shù):
E.g.:
? bool flag = mList.TrueForAll(ListFind);?//委托給ListFind函數(shù)
Console.WriteLine("True for all:??"+flag);??//flag值為false
這兩種方法的結(jié)果是一樣的。
List.FindAll方法:檢索與指定謂詞所定義的條件相匹配的所有元素。
public List<T> FindAll(Predicate<T> match);
E.g.:
List<string> subList = mList.FindAll(ListFind);?//委托給ListFind函數(shù)
??????? 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 in takeList)
??????????{
????????????? 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)
????????????????????{
??????????????????????? return true;
????????????????????}
??????????????????? else
????????????????????{
??????????????????????? return false;
????????????????????}
????????????????});
???????? foreach (string s in subList)
?????????{
???????????? Console.WriteLine("element in subList:?"+s);
?????????}
?????????這時subList存儲的就是所有長度大于3的元素
List.RemoveAll方法:移除與指定的謂詞所定義的條件相匹配的所有元素。
public int RemoveAll(Predicate<T> match);
E.g.:
??????????? mList.RemoveAll(name =>
????????????????{
??????????????????? if (name.Length?> 3)
????????????????????{
??????????????????????? return true;
????????????????????}
??????????????????? else
????????????????????{
??????????????????????? return false;
????????????????????}
????????????????});
??????????? foreach (string s in mList)
????????????{
??????????????? Console.WriteLine("element in mList:?????"?+ s);
????????????}
??????這時mList存儲的就是移除長度大于3之后的元素。
List?是一個泛型鏈表...T表示節(jié)點元素類型
比如
List intList;表示一個元素為int的鏈表
intList.Add(34);?//添加
intList.Remove(34);//刪除
intList.RemoveAt(0);?//刪除位于某處的元素
intList.Count;?//鏈表長度
還有Insert,Find,FindAll,Contains等方法,也有索引方法 intList[0]?= 23;
1.減少了裝箱拆箱
2.便于編譯時檢查數(shù)據(jù)類型
List?就相當(dāng)于 System.Collections命名空間里面的List
引自killer的博客,原文:
http://www.cnblogs.com/killers/archive/2011/08/28/2155920.html
感謝作者!未查看到版權(quán)信息,如有侵權(quán),請告知。