宣告:
1、listmlist = new list();
t為列表中元素型別,現在以string型別作為例子
e.g.:listmlist = new list();
2、listtestlist =new list(ienumerablecollection);
以乙個集合作為引數建立list
e.g.:
string temarr = ;
listtestlist = new list(temarr);
1.新增元素
(1)list. add(t item) 新增乙個元素,e.g.: mlist.add("john");
(2)list. addrange(ienumerablecollection) 新增一組元素,
e.g.: string temarr = ;
mlist.addrange(temarr);
(3)在index位置新增乙個元素, e.g.: mlist.insert(1, "hei");
2.刪除元素
(1)刪除乙個值,e.g.: mlist.remove("hunter");
(2)刪除下標為index的元素,e.g.: mlist.removeat(0);
(3)從下標index開始,刪除count個元素, e.g.: mlist.removerange(3, 2);
3.排序元素
(1)預設是元素第乙個字母按公升序,e.g.: mlist.sort();
(2)順序反轉,e.g.:list. reverse () ;可以與list. sort ()配合使用,達到想要的效果
4.查詢元素
(1)list.find方法:搜尋與指定謂詞所定義的條件相匹配的元素,並返回整個 list 中的第乙個匹配元素。
predicate是對方法的委託,如果傳遞給它的物件與委託中定義的條件匹配,則該方法返回 true。當前 list 的元素被逐個傳遞給predicate委託,並在 list 中向前移動,從第乙個元素開始,到最後乙個元素結束。當找到匹配項時處理即停止。
public t find(predicatematch);
委託給拉姆達表示式:
e.g.:
1 stringlistfind = mlist.find(name => )//name是變數,代表的是mlist委託給乙個函式:2 3
9 10 returnfalse;
11 12 };
13 14
15 16 console.writeline(listfind); //輸出是hunter
1 string listfind1 = mlist.find(listfind); //委託給listfind函式這兩種方法的結果是一樣的。2 console.writeline(listfind); //輸出是hunter
3 4 //listfind函式:
5 public bool listfind(string name)
6
11 returnfalse;
12 }
list.findlast方法:搜尋與指定謂詞所定義的條件相匹配的元素,並返回整個 list 中的最後乙個匹配元素。
(2)list.trueforall方法: 確定是否 list 中的每個元素都與指定的謂詞所定義的條件相匹配。
publicbool trueforall(predicatematch);
委託給拉姆達表示式:
1 bool flag=mlist.trueforall(name=>)委託給乙個函式,這裡用到上面的listfind函式:2 7 else
8 11 }
12 console.writeline("true for all:"+flag);//flag 的值為false
1 bool flag = mlist.trueforall(listfind); //委託給listfind函式(3)list.findall方法:檢索與指定謂詞所定義的條件相匹配的所有元素。2 console.writeline("true forall: "+flag); //flag值為false
publiclistfindall(predicatematch);
1 e.g.:(3)list.where方法:檢索與指定謂詞所定義的條件相匹配的所有元素。跟list.findall方法類似。2 3 listsublist =mlist.findall(listfind); //委託給listfind函式
4 5 foreach (string s in sublist)
6 7
12 13 //這時sublist儲存的就是所有長度大於3的元素
14 15 獲得前n行 返回值為ienumetable,t的型別與list的類//型一樣
16 17 e.g.:
18 19 ienumerabletakelist= mlist.take(5);
20 21 foreach(string s intakelist)
22 23
28 29 // 這時takelist存放的元素就是mlist中的前5個
(4)list.removeall方法:移除與指定的謂詞所定義的條件相匹配的所有元素。
c 中的list用法
include include include include using namespace std 建立乙個list容器的例項listint typedef list listint 建立乙個list容器的例項listchar typedef list listchar void main vo...
C 中list的用法
list的底層結構 list 是可以隨意插入和刪除的序列式容器,list底層結構是帶頭結點雙向迴圈鍊錶 list中常用介面說明 1.list的構造 list 構造空的list list size t n,const t data t 構造的list中包含n個值為data的元素 list first,...
C 中list用法詳解
1.關於list容器 list是一種序列式容器。list容器完成的功能實際上和資料結構中的雙向鍊錶是極其相似的,list中的資料元素是通過鍊錶指標串連成邏輯意義上的線性表,也就是list也具有鍊錶的主要優點,即 在鍊錶的任一位置進行元素的插入 刪除操作都是快速的。list的實現大概是這樣的 list...