90 lines
2.6 KiB
C#
90 lines
2.6 KiB
C#
|
|
using System.Collections;
|
||
|
|
|
||
|
|
namespace Lesson3_Queue
|
||
|
|
{
|
||
|
|
class Test
|
||
|
|
{
|
||
|
|
|
||
|
|
}
|
||
|
|
internal class Program
|
||
|
|
{
|
||
|
|
static void Main(string[] args)
|
||
|
|
{
|
||
|
|
#region 知识点一 队列本质
|
||
|
|
//queue是C#为我们封装好的类
|
||
|
|
//本质是一个Object数组,只是封装了特殊的存储规则
|
||
|
|
|
||
|
|
//queue是队列存储容器,队列是一种先进先出的数据结构
|
||
|
|
//先存入的数据先取出,后存入的数据后取出
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
#region 知识点二 声明
|
||
|
|
//需要引用命名空间system.collections
|
||
|
|
Queue queue = new Queue();
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
#region 知识点三 增取查改
|
||
|
|
|
||
|
|
//增
|
||
|
|
queue.Enqueue(1);//入队
|
||
|
|
queue.Enqueue("123");
|
||
|
|
queue.Enqueue(1.4f);
|
||
|
|
queue.Enqueue(new Test());
|
||
|
|
|
||
|
|
//取
|
||
|
|
object v = queue.Dequeue();//出队,取出队列头部的元素
|
||
|
|
Console.WriteLine(v);
|
||
|
|
v = queue.Dequeue();
|
||
|
|
Console.WriteLine(v);
|
||
|
|
|
||
|
|
//查
|
||
|
|
//1.查看队列头部的元素
|
||
|
|
v = queue.Peek();
|
||
|
|
Console.WriteLine(v);
|
||
|
|
v = queue.Peek();
|
||
|
|
Console.WriteLine(v);
|
||
|
|
//2.查看元素是否存在于队列中
|
||
|
|
if(queue.Contains(1.4f))
|
||
|
|
{
|
||
|
|
Console.WriteLine("存在1.4f");
|
||
|
|
}
|
||
|
|
|
||
|
|
//改
|
||
|
|
//队列没有提供修改元素的方法
|
||
|
|
//只能通过出队和入队的方式来间接修改元素
|
||
|
|
queue.Clear();
|
||
|
|
queue.Enqueue(1);
|
||
|
|
queue.Enqueue(2);
|
||
|
|
queue.Enqueue(3);
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
#region 知识点四 遍历
|
||
|
|
//1.长度
|
||
|
|
Console.WriteLine(queue.Count);
|
||
|
|
//2.遍历
|
||
|
|
foreach (object item in queue)
|
||
|
|
{
|
||
|
|
Console.WriteLine(item);
|
||
|
|
}
|
||
|
|
//3.转数组
|
||
|
|
object[] arr = queue.ToArray();
|
||
|
|
for(int i = 0; i < arr.Length; i++)
|
||
|
|
{
|
||
|
|
Console.WriteLine(arr[i]);
|
||
|
|
}
|
||
|
|
//4.循环出队
|
||
|
|
while(queue.Count > 0)
|
||
|
|
{
|
||
|
|
Console.WriteLine(queue.Dequeue());
|
||
|
|
}
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
#region 知识点五
|
||
|
|
// 由于用万物之父来存储数据,自然存在装箱拆箱
|
||
|
|
// 当往其中进行值类型存储时就是在装箱
|
||
|
|
// 当将值类型对象取出来转换使用时,就存在拆箱
|
||
|
|
#endregion
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|