zoukankan      html  css  js  c++  java
  • c#Queue队列使用方法

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace Queue测试
    {
    class Program
    {
    static void Main(string[] args)
    {
    Queue<string> strList = new Queue<string>();
    ///向队列加入元素
    strList.Enqueue("元素1");
    strList.Enqueue("元素2");
    strList.Enqueue("元素3");
    ///遍历元素
    foreach (var item in strList)
    {
    Console.WriteLine(item);
    }
    ///队长长度
    Console.Write("队列长度---");
    Console.WriteLine(strList.Count);
    ////取出最先加进去的元素,并删除,充分体现队列的先进先出的特性
    ///如队列中无元素,则会引发异常
    //string mes = strList.Dequeue();
    //Console.WriteLine(mes);
    
    ///取出最先入队的元素,但并不删除
    string mes = strList.Peek();
    Console.Write("取出但不移除队头的元素----");
    Console.WriteLine(mes);
    ///遍历队列,仍为三个元素
    Console.WriteLine("队列剩余元素为---");
    foreach (var item in strList)
    {
    Console.WriteLine(item);
    }
    
    ///直接获得队列中的某个元素,
    ///如果索引越界,会引发异常
    Console.Write("获取队列中的第2个元素----");
    string s = strList.ElementAt(2);
    Console.WriteLine(s);
    ///直接获得队列中的某个元素,
    ///如果索引越界,则会返回null,但不引发异常
    Console.Write("获取队列中的第5个元素-----");
    Console.WriteLine( strList.ElementAtOrDefault(5));
    Console.Write("获取队列中的第1个元素-----");
    Console.WriteLine(strList.ElementAtOrDefault(1));
    
    ///取出最先入队的元素,但并不删除
    string pop = strList.Dequeue();
    Console.Write("取出队头的元素----");
    Console.WriteLine(mes);
    ///遍历队列,为2个元素
    Console.WriteLine("队列剩余元素为---");
    foreach (var item in strList)
    {
    Console.WriteLine(item);
    }
    Console.ReadKey();
    
    }
    }
    }
    strList.clear();//用户删除所有对象,清空数据

    欢迎讨论,相互学习。 txwtech@163.com
  • 相关阅读:
    javafx DragDropped file
    javafx style and cssFile
    javafx ComboBox Event and change cell color
    javafx clipboard
    javafx Cursor
    javafx DropShadow
    javafx checkbox
    javafx image button
    GNS3连接虚拟机
    cain使用教程
  • 原文地址:https://www.cnblogs.com/txwtech/p/14864603.html
Copyright © 2011-2022 走看看