泛型集合类小测试代码

 

//泛型集合类小测试代码

       private Dictionary<int,string> frontHashTable ;// = new Dictionary<int,string>() ;
        private List<string> myList;
        private Stack<string> myStack;
        private Queue<string> myQueue;
        private SortedList<int,string> mySortedList;

        public Form1()
        {
            InitializeComponent();
            frontHashTable = frontHashTable = new Dictionary<int, string>();
            myList = new List<string>();
            myStack = new Stack<string>();
            myQueue = new Queue<string>();
            mySortedList = new SortedList<int,string>();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();

            frontHashTable.Clear();
            frontHashTable.Add(1, "近光灯");
            frontHashTable.Add(2, "远光灯");
            frontHashTable.Add(3, "前雾灯");
            frontHashTable.Add(4, "后雾灯");

           
            foreach (KeyValuePair<int,string> kp in frontHashTable)
            {
                if (kp.Key % 7 != 0)
                {
                    listBox1.Items.Add(kp.Value);
                }
            }

            listBox1.Items.Add("********");

            foreach (string s in frontHashTable.Values)
            {
                listBox1.Items.Add(s);
            }


            //以下用List泛型方法
            listBox1.Items.Clear();
            myList.Clear();
            richTextBox1.Clear();

            myList.Add("左转向灯");
            myList.Add("右转向灯");
            myList.Add("挂车左转向灯");
            myList.Add("挂车右转向灯");
            myList.Add("仪表小灯");

            //listBox1.DataSource = myList;

            foreach (string s in myList)
            {
                richTextBox1.Text += s + "/r/n";
                listBox1.Items.Add(s);
            }


            //以下用Stack泛型方法

            myStack.Clear();
            listBox1.Items.Clear();
            richTextBox1.Clear();

            myStack.Push("近光灯");
            myStack.Push("远光灯");
            myStack.Push("前雾灯");
            myStack.Push("后雾灯");
            myStack.Push("仪表小灯");

            foreach (string s in myStack)
            {
                listBox1.Items.Add(s);
            }
            listBox1.Items.Add("*******************");
            myStack.Pop();

            foreach (string s in myStack)
            {
                listBox1.Items.Add(s);
            }


            //以下用Queue泛型方法
            myQueue.Clear();
            //listBox1.Items.Clear();
            richTextBox1.Clear();

            myQueue.Enqueue("近光灯");
            myQueue.Enqueue("远光灯");
            myQueue.Enqueue("前雾灯");
            myQueue.Enqueue("后雾灯");
            myQueue.Enqueue("仪表小灯");
           
            foreach (string s in myQueue)
            {
                richTextBox1.Text += s + "/r/n";
            }

            richTextBox1.Text += "****************" + "/r/n";

            myQueue.Dequeue();

            foreach (string s in myQueue)
            {
                richTextBox1.Text += s + "/r/n";
            }

            //以下应用SortedList泛型方法
            mySortedList.Clear();
            listBox1.Items.Clear();
            richTextBox1.Clear();

            mySortedList.Add(5,"前灯");
            mySortedList.Add(2, "后灯");
            mySortedList.Add(1, "前雾");
            mySortedList.Add(4, "后雾");
            mySortedList.Add(9, "仪表小灯");


            foreach (KeyValuePair<int ,string> de in mySortedList)
            {
                if (de.Key % 10 != 0)
                {
                    listBox1.Items.Add(de.Value);
                }
            }

        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值