【问题标题】:number of indices exceeds the number of dimensions of the indexed array vb索引数超过索引数组 vb 的维数
【发布时间】:2018-06-22 16:43:42
【问题描述】:

在我调用函数的行的 sub main 中,我遇到了一个我不知道如何解决的错误:

索引数超过索引数组的维数

Module Module1
    Sub main()
        Dim names() As String = {"heimdall", "hella", "loki", "thor", "tyr", "odin"} 'list in order
        Dim last As Integer = names.Length - 1
        Dim first As Integer = 0
        Dim player As String = "thor"

        search(names(), last, first, player)

    End Sub

    Function search(ByVal names() As String, ByVal last As Integer, ByVal first As Integer, ByVal player As String)
        Dim midpoint As Integer = (first + last) \ 2
        Dim found As Boolean = False

        While found = False
            If last < first Then
                Return -1
            End If

            If names(midpoint) > player Then
                Return search(names, last, first, midpoint)
                found = True
            Else
                Return midpoint
                found = True
            End If
        End While


        Return midpoint 'automatically does this when found = true

    End Function

End Module

【问题讨论】:

  • 如果你打开 Option Strict 你会发现你的函数需要一个数据类型。此外,当您在第二个 Return 语句中递归调用函数时,您传递的中点数据类型错误。

标签: vb.net recursion binary-search


【解决方案1】:

names() 末尾的括号传递给search 方法时删除它。只有在声明数组时才需要这些。

search(names, last, first, player)

【讨论】:

    猜你喜欢
    • 2023-04-11
    • 2017-07-17
    • 1970-01-01
    • 1970-01-01
    • 2018-01-22
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多