Unity C# Basic Review 17 - Queue (P379-380)

 /* 
     * First-in, first-out object collection, special sequence table
     * 
     * Queue (Queue)
     * Objects follow the principle of first-in, first-out, first-come, first-served
     * 
     * Objects are stored in a buffer of default size 32 in order; when When the buffer space is insufficient, create a new buffer according to the growth factor (2.0)
     * and copy the existing objects to the new buffer (high overhead)
     * 
     * Methods of Queue
     * Enqueue Enter the queue Enter the queue
     * Dequeue Dequeue Leave the queue
     * Peek to view the head of the queue
     * Clear to clear the queue
     * Contains to ask whether it contains
     * Count the number of elements in the queue
     */

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    /* 
     * 先进先出的对象集合、特殊的顺序表
     * 
     * 队列(Queue)
     * 对象按照先进先出,先来先服务的原则
     * 
     * 对象按顺序存储在默认大小的32的缓冲区中;当缓冲区空间不足时,按增长因子(2.0)创建一个新的缓冲区
     * 并将现有对象拷贝到新缓冲区中(开销大)
     * 
     * Queue的方法
     * Enqueue   入队  进队
     * Dequeue   出队  离队
     * Peek  查看队头
     * Clear  清除队列
     * Contains  询问是否包含
     * Count   队列中的元素个数
     */

    public static void QueueTest()
    {
        Queue queue = new Queue();
        for (int i = 0; i < 10; i++)
        {
            queue.Enqueue("第" + (i + 1) + "个人");
        }

        //出队为队中数据进行服务
        while (queue.Count != 0)
        {
            Debug.Log("为" + queue.Dequeue() + "服务");
        }
    }
    void Start()
    {
        QueueTest();
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    /* 
     * 先进先出的对象集合、特殊的顺序表
     * 
     * 队列(Queue)
     * 对象按照先进先出,先来先服务的原则
     * 
     * 对象按顺序存储在默认大小的32的缓冲区中;当缓冲区空间不足时,按增长因子(2.0)创建一个新的缓冲区
     * 并将现有对象拷贝到新缓冲区中(开销大)
     * 
     * Queue的方法
     * Enqueue   入队  进队
     * Dequeue   出队  离队
     * Peek  查看队头
     * Clear  清除队列
     * Contains  询问是否包含
     * Count   队列中的元素个数
     */

    public static void QueueTest()
    {
        Queue queue = new Queue();
        for (int i = 0; i < 10; i++)
        {
            queue.Enqueue("第" + (i + 1) + "个人");
        }

        //出队为队中数据进行服务
        while (queue.Count != 0)
        {
            if (queue.Peek().Equals("第5个人")) 
            {
                Debug.Log("请准备!");
            }
            System.Object man = queue.Dequeue();
            if (man.Equals("第5个人"))
            {
                Debug.Log("为" + man + "唱生日快乐歌");
            }
            else
            {
                Debug.Log("为" + man + "服务");
            }

        }
    }
    void Start()
    {
        QueueTest();
    }
}

 

Guess you like

Origin blog.csdn.net/weixin_46711336/article/details/124501594