Vb.net 用visualbasic实现堆栈
我尝试在VB.NET中实现一个堆栈类。我遇到的问题是在以下行中出现错误:Vb.net 用visualbasic实现堆栈,vb.net,stack,Vb.net,Stack,我尝试在VB.NET中实现一个堆栈类。我遇到的问题是在以下行中出现错误:Console.WriteLine(Stack1.push(item,top,maxSize,s1(top))“ 错误内容为:“System.IndexOutOfRangeException:”索引超出了数组的界限” 感谢您的帮助,以下是完整的代码解决方案: Class Stack Public Function isEmpty(top As Integer) As Boolean If top =
Console.WriteLine(Stack1.push(item,top,maxSize,s1(top))
“
错误内容为:“System.IndexOutOfRangeException:”索引超出了数组的界限
”
感谢您的帮助,以下是完整的代码解决方案:
Class Stack
Public Function isEmpty(top As Integer) As Boolean
If top = -1 Then
Return True
Else
Return False
End If
End Function
Public Function isFull(top As Integer, maxSize As Integer) As Boolean
If top = maxSize Then
Return True
Else
Return False
End If
End Function
Public Function push(item As String, top As Integer, maxSize As Integer, ByVal ParamArray s() As String)
If isFull(top, maxSize) = True Then
Console.WriteLine("Stack is full")
Else
top += 1
s(top) = item
End If
Return s(top)
End Function
Public Function pop(item As String, top As Integer, ByVal ParamArray s() As String)
If isEmpty(top) Then
Console.WriteLine("Stack is Empty")
Else
item = s(top)
top -= 1
End If
Return top & item
End Function
End Class
Sub Main()
Dim Stack1 As Stack = New Stack()
Dim maxSize As Integer = 5
Dim s1(maxSize) As String
Dim top As Integer = -1
Dim item As String = "Erica"
Console.WriteLine(Stack1.push(item, top, maxSize, s1(top)))
End Sub
如果您不知道,.NET有一个。它看起来像top=-1,所以第四个parm就是问题。
ParamArray
与数组不同,这就是问题所在。@BrianMStafford我知道.NET框架中内置了一个堆栈类,但我正在尝试实现一个,以查看是否可以重新创建它。谢谢您的建议,我将研究它第四个参数。@laancelot所以我需要把byVal数组s()作为字符串
?在你的声明中,去掉Array
(甚至是byVal
),你应该很好。回答一个类似的问题。