为了账号安全,请及时绑定邮箱和手机立即绑定

有关 Hashtable 和 SortedList

有关 Hashtable 和 SortedList

叮当猫咪 2018-11-20 04:01:36
其实是想要一个不定项的数组,有Key 和 Value,在遍历的时候符合先进先出,就是遍历出来的顺序与Add的顺序是一致的。而用 Hashtable 在遍历的时候会随机排列,因为 Hashtable 本事就是无序的;用 SortedList 在遍历的时候又会根据 Key 的字母顺序排列最终 的结果(用在我的需求上画蛇添足了);有没有其他什么对象可以实现我这个简单的功能,难道要用数组来模拟吗?
查看完整描述

2 回答

?
12345678_0001

TA贡献1802条经验 获得超5个赞

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    !
*/


查看完整回答
反对 回复 2018-11-21
?
临摹微笑

TA贡献1982条经验 获得超2个赞

遍历出来的顺序与Add的顺序是一致的
先进先出(FIFO, First in first out),明显该用队列,你就封装个类存进Queue好了,或者就直接存KeyValuePair吧

查看完整回答
反对 回复 2018-11-21
  • 2 回答
  • 0 关注
  • 406 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号