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