其实是想要一个不定项的数组,有Key 和 Value,在遍历的时候符合先进先出,就是遍历出来的顺序与Add的顺序是一致的。而用 Hashtable 在遍历的时候会随机排列,因为 Hashtable 本事就是无序的;用 SortedList 在遍历的时候又会根据 Key 的字母顺序排列最终 的结果(用在我的需求上画蛇添足了);有没有其他什么对象可以实现我这个简单的功能,难道要用数组来模拟吗?
2 回答
弑天下
TA贡献1818条经验 获得超8个赞
Queue队列
你存的元素改为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贡献1784条经验 获得超7个赞
遍历出来的顺序与Add的顺序是一致的
先进先出(FIFO, First in first out),明显该用队列,你就封装个类存进Queue好了,或者就直接存KeyValuePair吧
- 2 回答
- 0 关注
- 566 浏览
添加回答
举报
0/150
提交
取消
