1、构造函数之间相互调用
Me.New(别的带参数的构造函数的重载版本)
Code3-2
Queue.vb:
Public Class Queue
Private queueArray() As Object
Private growFactor As Integer
Private head As Integer
Private Const MinimumGrow As Integer = 4
Private Const ShrinkThreshold As Integer = &H20 '初始容量可以存放32个元素
Private size As Integer
Private tail As Integer
Public ReadOnly Property count As Integer
Get
Return size
End Get
End Property
Sub New()
Me.New(ShrinkThreshold, 2.0F)
End Sub
Sub New(ByVal capacity As Integer, ByVal growFactor As Single)
If capacity < 0 Then
Throw New ArgumentOutOfRangeException("capacity", "初始容量不能小于0")
End If
If (growFactor < 1.0 Or growFactor > 10.0) Then
Throw New ArgumentOutOfRangeException("groFactor", "增长因子必须在1到10之间")
End If
queueArray = New Object(capacity) {}
head = 0
tail = 0
size = 0
Me.growFactor = Integer.Parse(growFactor * 100.0F)
End Sub
'出队
Public Function Dequeue() As Object
If size = 0 Then
Throw New InvalidOperationException("队列为空")
End If
Dim obj2 As Object = queueArray(head)
queueArray(head) = Nothing
head = (head + 1) Mod queueArray.Length
size -= 1
Return obj2
End Function
'入队
Public Sub Enqueue(ByVal obj As Object)
If size = queueArray.Length Then
Dim capacity As Integer
capacity = Integer.Parse((queueArray.Length * growFactor) / 100L)
If capacity < (queueArray.Length + MinimumGrow) Then
capacity = queueArray.Length + MinimumGrow
End If
Call SetCapacity(capacity)
End If
queueArray(tail) = obj
tail = (tail + 1) Mod queueArray.Length
size += 1
End Sub
Private Sub SetCapacity(ByVal capacity As Integer)
Dim destinationArray() As Object = New Object(capacity) {}
If head < tail Then
Array.Copy(queueArray, head, destinationArray, 0, size)
Else
Array.Copy(queueArray, head, destinationArray, 0, queueArray.Length - head)
Array.Copy(queueArray, 0, destinationArray, queueArray.Length - head, tail)
End If
queueArray = destinationArray
head = 0
tail = IIf(size = capacity, 0, size)
End Sub
End Class
Module1.vb:
Module Module1
Sub Main()
Dim newQueue As New Queue
Dim left As Integer = 0
Dim right As Integer = 0
Console.Write("请输入行数:")
Dim n As Integer = Integer.Parse(Console.ReadLine)
For i As Integer = 0 To n - 1
For j As Integer = 0 To n - 1 - 1
Console.Write(" ")
Next
For k As Integer = 0 To i
Dim num As Integer = 1
If k <> i Then
right = CType(newQueue.Dequeue, Integer)
If k <> 0 Then
num = left + right
End If
left = right
End If
Console.Write(String.Format("{0,-4}", num.ToString))
newQueue.Enqueue(num)
Next
Console.WriteLine()
Next
Console.ReadKey()
End Sub
End Module