其實(shí)是想要一個(gè)不定項(xiàng)的數(shù)組,有Key 和 Value,在遍歷的時(shí)候符合先進(jìn)先出,就是遍歷出來的順序與Add的順序是一致的。而用 Hashtable 在遍歷的時(shí)候會隨機(jī)排列,因?yàn)?Hashtable 本事就是無序的;用 SortedList 在遍歷的時(shí)候又會根據(jù) Key 的字母順序排列最終 的結(jié)果(用在我的需求上畫蛇添足了);有沒有其他什么對象可以實(shí)現(xiàn)我這個(gè)簡單的功能,難道要用數(shù)組來模擬嗎?
2 回答

弒天下
TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超8個(gè)贊
Queue隊(duì)列
你存的元素改為KeyValuePair或自定義的Key-Value對的類即可
命名空間: System.Collections
[object Object]Code using System; using System.Collections; public class SamplesQueue { public static void Main() { // Creates and initializes a new Queue. Queue myQ = new Queue(); myQ.Enqueue("Hello"); myQ.Enqueue("World"); myQ.Enqueue("!"); // Displays the properties and values of the Queue. Console.WriteLine( "myQ" ); Console.WriteLine( "\tCount: {0}", myQ.Count ); Console.Write( "\tValues:" ); PrintValues( myQ ); } public static void PrintValues( IEnumerable myCollection ) { foreach ( Object obj in myCollection ) Console.Write( " {0}", obj ); Console.WriteLine(); } } /* This code produces the following output. myQ Count: 3 Values: Hello World ! */

嚕嚕噠
TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超7個(gè)贊
遍歷出來的順序與Add的順序是一致的
先進(jìn)先出(FIFO, First in first out),明顯該用隊(duì)列,你就封裝個(gè)類存進(jìn)Queue好了,或者就直接存KeyValuePair吧
- 2 回答
- 0 關(guān)注
- 516 瀏覽
添加回答
舉報(bào)
0/150
提交
取消