队列(简称为队)是一种先入先出(First In, First Out)的数据结构。
环境要求
Windows XP
及以上。Windows 10
、Windows 11
在Windows 功能
中勾选.NET Framework 3.5 (包括 .NET 2.0 和 3.0)
。
前置知识
WSH.Echo Empty = Empty
-1
WSH.Echo Null = Null
null
WSH.Echo New RegExp Is New RegExp
0
Set oRE = New RegExp
WSH.Echo oRE Is oRE
-1
WSH.Echo CreateObject("Scripting.FileSystemObject") Is CreateObject("Scripting.FileSystemObject")
0
Set oFS = CreateObject("Scripting.FileSystemObject")
WSH.Echo oFS Is oFS
-1
下面两个返回值出现的原因是浮点误差:
WSH.Echo 0.1 + 0.2 = 0.3
0
WSH.Echo 100000000000000000000000 = 100000000000000000000001
-1
使用
创建一个 Queue
对象:
Set oQ = CreateObject("System.Collections.Queue")
Enqueue 方法:将元素入队
Set oQ = CreateObject("System.Collections.Queue")
oQ.Enqueue Empty
oQ.Enqueue Null
oQ.Enqueue "String"
oQ.Enqueue 0
oQ.Enqueue 3.14
oQ.Enqueue CreateObject("Scripting.FileSystemObject")
oQ.Enqueue New RegExp
oQ.Enqueue True
oQ.Enqueue False
Count 属性:表示当前队内元素个数
Set oQ = CreateObject("System.Collections.Queue")
WSH.Echo oQ.Count()
0
oQ.Enqueue 666
WSH.Echo oQ.Count()
1
Clear 方法:清空队列
Set oQ = CreateObject("System.Collections.Queue")
oQ.Enqueue 888
WSH.Echo oQ.Count()
1
oQ.Clear
WSH.Echo oQ.Count()
0
Clone 方法:返回该队列的拷贝
Set oQ = CreateObject("System.Collections.Queue")
oQ.Enqueue 666
Set oQ2 = oQ.Clone()
oQ2.Enqueue 888
WSH.Echo oQ Is oQ2, oQ.Count(), oQ2.Count()
0 1 2
Set oQ = CreateObject("System.Collections.Queue")
oQ.Enqueue 666
Set oQ2 = oQ
oQ2.Enqueue 888
WSH.Echo oQ Is oQ2, oQ.Count(), oQ2.Count()
-1 2 2
Set oQ = CreateObject("System.Collections.Queue")
oQ.Enqueue CreateObject("Scripting.FileSystemObject")
Set oQ2 = oQ.Clone
WSH.Echo oQ Is oQ2, oQ.Peek() Is oQ2.Peek()
0 -1
Set oQ = CreateObject("System.Collections.Queue")
oQ.Enqueue New RegExp
Set oQ2 = oQ.Clone
WSH.Echo oQ Is oQ2, oQ.Peek() Is oQ2.Peek()
0 -1
ToArray 方法:将队列转为普通 VBScript 数组
Set oQ = CreateObject("System.Collections.Queue")
oQ.Enqueue 1
oQ.Enqueue 3.1415926
oQ.Enqueue True
WSH.Echo Join(oQ.ToArray(), " ")
1 3.1415926 True
Set oQ = CreateObject("System.Collections.Queue")
oQ.Enqueue New RegExp
WSH.Echo oQ.ToArray()(0) Is oQ.Peek()
-1
Set oQ = CreateObject("System.Collections.Queue")
oQ.Enqueue CreateObject("Scripting.FileSystemObject")
WSH.Echo oQ.ToArray()(0) Is oQ.Peek()
-1
Contains 方法:检查队列内是否包含某元素
Set oQ = CreateObject("System.Collections.Queue")
oQ.Enqueue 1
oQ.Enqueue 2
WSH.Echo oQ.Contains(0), oQ.Contains(1)
0 -1
Set oQ = CreateObject("System.Collections.Queue")
oQ.Enqueue Null
oQ.Enqueue oQ
WSH.Echo oQ.Contains(Null), oQ.Contains(oQ), oQ.Contains(CreateObject("System.Collections.Queue"))
-1 -1 0
Peek 方法:返回队首的元素(但不从队列中移除)
Set oQ = CreateObject("System.Collections.Queue")
oQ.Enqueue 1
oQ.Enqueue 2
WSH.Echo oQ.Peek(), oQ.Peek()
1 1
WSH.Echo Join(oQ.ToArray(), " ")
1 2
Dequeue 方法:将队首元素出队
Set oQ = CreateObject("System.Collections.Queue")
oQ.Enqueue 1
oQ.Enqueue 2
WSH.Echo oQ.Dequeue(), oQ.Dequeue(), UBound(oQ.ToArray())
1 2 -1
GetHashCode 方法:返回队列的哈希码
Set oQ = CreateObject("System.Collections.Queue")
Set oQ2 = oQ
Set oQ3 = oQ.Clone()
Set oQ4 = CreateObject("System.Collections.Queue")
WSH.Echo oQ.GetHashCode(), oQ2.GetHashCode(), oQ3.GetHashCode(), oQ4.GetHashCode()
58225482 58225482 54267293 18643596
Equals 方法:确定是否为同一个队列
Set oQ = CreateObject("System.Collections.Queue")
Set oQ2 = oQ
Set oQ3 = oQ.Clone()
Set oQ4 = CreateObject("System.Collections.Queue")
WSH.Echo oQ.Equals(oQ), oQ.Equals(oQ2), oQ.Equals(oQ3), oQ.Equals(oQ4)
-1 -1 0 0
ToString 方法:返回类名
Set oQ = CreateObject("System.Collections.Queue")
WSH.Echo oQ.ToString(), TypeName(oQ)
System.Collections.Queue Queue