List<T> LIS = new List<T>();
int[] arr = { 9, 9, 9, 9, 9, };
list.addrange{ arr};
for (int i = 0; i < list.count; i++)
{
Console.WriteLine( list[i]+"");
插入元素
List.insert(0, 2);
在下表爲0的位置插入2
bool result = list.remove();//移除第一個匹配量元素 移除成功返回true
list.removeat(2);移除對應下標爲2的元素
console。writeline(rusule);
list.removerange( 2 , 3 );移除個數的指定的下標,指定的元素
removeall 移除所有元素
移除當前list中所有的偶數
list。removeall(it=>it%2==0)
翻轉
list.reverse
獲取第一個匹配元素的下標
int index=list。indexof(9);
lastindexof 獲取最後一個和指定元素匹配的下標
.Net中的List<>類的Sort方法有四種重載形式
1.不帶有任何參數的Sort方法----Sort();
2.帶有比較器參數的Sort方法 ----Sort(IComparer<T>)
3.帶有比較代理方法參數的Sort方法----Sort(Comparison<(Of <(T>)>))
4.帶有比較起參數,可以指定排序範圍的Sort方法----Sort(Int32, Int32 IComparer(T))
較爲常見的一種:
List.Sort((a,b)=>{a.CompareTo(b)});
就是上述List<>類的第三種形式,參數裏提供了比較代理方法,其中比較代理方法用lamda表達式。
①不帶有任何參數的Sort方法----Sort():
使用這種方法不是對List中的任何元素對象都可以進行排序,List中的元素對象必須繼承IComparable接口,並且要實現IComparable接口中的CompareTo()方法,在CompareTo()方法中要自己實現對象的比較規則。
②帶有比較器參數的Sort方法 ----Sort(IComparer<T>):
List中的元素對象不需要繼承IComparable接口,但需要額外創建一個對象的比較器,必須繼承IComparer<T>接口,並且實現接口中的Compare()方法。
③帶有比較代理方法參數的Sort方法----Sort(Comparison<(Of <(T>)>)):
此方法需要編寫一個對象排序比較的方法,對List中的元素對象沒有特殊的要求,但在比較方法中需要實現對象比較
④帶有比較起參數,可以指定排序範圍的Sort方法----Sort(Int32, Int32 IComparer(T)):
此方法實際是第二種比較器排序的一個擴展,在指定排序比較器的同時,指定排序範圍,即List中準備排序的開始元素索引和結束元素索引。