C 學習日記 列表List

2021-07-29 22:52:17 字數 3610 閱讀 1791

using system;

using system.collections.generic;

namespace sample

; racers.add(new racer(14, "niki", "lauda", "austria", 25));

racers.addrange(new racer

);racers.insert(3,new racer(14,"joyn","wu","china",3));

//foreach方法括號中應該放方法位址或者lambda表示式

//public delegate void action(t obj)沒有返回值 引數是t型別的lambda

=> console.writeline("",r));

/*for(int i = 0; i < racers.count; i++)

",racers[i]);

}*///兩種移除的方法 第一種是根據索引的位置 一種是根據元素物件 第二種實際上還是第一種因為第二種會先用indexof()方法尋找元素的索引

/*racers.removeat(3);

if(!racers.remove(july))

console.writeline("object not found in collection.");

foreach(racer r in racers)

", r);

}*///運用自己寫的類尋找符合國家相同的第乙個元素的索引

//這個類中有乙個引數儲存國家資訊

//還有乙個方法匹配該物件的國家是否一致的方法返回布林型別

/*int index1 = racers.findindex((new findcountry("china")).findcountrypredicate);

console.writeline(index1);

//也可以寫lambda表帶式進行匹配

//public delegate bool predicate(t obj)返回值為布林型別 引數為t型別

int index2 = racers.findindex(r => r.country == "china");

int index3 = racers.findindex(r => r.lastname == "wu");

console.writeline(index2);

console.writeline(index3);

//找到符合條件的所有項 返回乙個集合

//前面的方法都是返回匹配的第一項就不再搜尋了

listbigwinners = racers.findall(r => r.wins > 20);

foreach(racer r in bigwinners)

",r);

}*///實現了icomparebale介面 會自動的呼叫compareto()方法進行排序

/*racers.sort();

foreach(racer r in racers)

",r);

}console.writeline("-------------------");

//public delegate int comparison(t x, t y)返回值是int 有兩個型別為t的引數

racers.sort((r1, r2) => r1.wins.compareto(r2.wins));

foreach(racer r in racers)

",r);

}//sort()方法裡傳遞乙個實現了icomparer介面的類的例項

//根據傳遞進來的比較型別 進行比較 排序

console.writeline("-------------------");

racers.sort(new racercomparer(racercomparer.comparetype.country));

foreach(racer r in racers)

",r);

}console.writeline("-------------------");*/

//乙個集合往另乙個集合進行轉換

//public sealed delegate toutput convert(tinput from);

listpersons = racers.convertall(r => new person(r.firstname+" "+r.lastname));

foreach(person p in persons)

return 0;

} }[serializable]

public class person

public override string tostring()

}[serializable]

public class racer : icomparable,iformattable

public string firstname

public string lastname

public string country

public int wins

public racer(int id, string firstname,string lastname, string country)

:this(id,firstname,lastname,country,0)

public racer(int id, string firstname, string lastname, string country, int wins)

public override string tostring()

",firstname,lastname);

} public string tostring(string format,iformatprovider formatprovider)

,wins:",tostring(),wins);

case "c":

return string.format(",country:",tostring(),country);

case "a":

return string.format(",country:,wins:",tostring(),country,wins);

default:

throw new formatexception(string.format(formatprovider,"format is not supported",format));

}} public string tostring(string format)

public int compareto(racer other)

}public class findcountry

public bool findcountrypredicate(racer r)

}public class racercomparer : icomparer

private comparetype comparetype;

public racercomparer(racercomparer.comparetype type)

public int compare(racer r1, racer r2)

}}}

list列表學習

鍊錶的核心是乙個雙向鍊錶 list的資料元素是通過指標串聯成的邏輯意義上的線性表 優點 在鍊錶的任一位置進行元素的插入 刪除操作都是快速的。list每個節點有三個域 前驅元素指標域 資料域 後繼元素指標域。頭節點的前驅元素指標域儲存的是尾元素的首位址,尾結點的後繼元素指標域儲存的是頭節點的首位址。l...

學習STL 列表list

vector容器提供了對元素的快速隨機訪問,但是付出的代價是在其任意位置插入和刪除元素,比在容器尾部插入和刪除的開銷更大。與vector相比,list容器可以在任何位置快速插入和刪除,但是付出的代價是元素的隨機訪問開銷更大。採用list的結構實現容器的基本操作與vector容器類似。list 容器與...

Python學習日記 二 list操作

l a b c d 1,2,3,e 4 在list的結尾新增乙個新的元素,沒有返回值,但會修改原列表 print l output a b c d 1,2,3,e 4 5 2.list.insert list.insert index,value 在指定的index插入乙個新的元素 l.insert...