【问题标题】:Number of indices is less than the number of dimensions of the indexed array索引数小于索引数组的维数
【发布时间】:2014-11-09 20:15:33
【问题描述】:
    Dim tableautemp() As String = IO.File.ReadAllLines(nomfichier)
    Dim etudianttemp() As String

    For i As Integer = 0 To tableautemp.Length - 1
        etudianttemp() = tableautemp(i).Split(";"c)
        For j As Integer = 0 To 6
            tableau(j, i) = etudianttemp(j)
        Next
    Next

我想读取文件并将行放在一维选项卡中,然后将每一行拆分到另一个一维选项卡中,然后将所有内容添加到二维选项卡中。但我得到“索引数小于索引数组的维数”。我不明白:s

【问题讨论】:

  • 不客气。另外,由于您是 StackOverflow 的新手,我想通知您,您可以通过勾选答案旁边的勾号来为好的答案投票并接受对您帮助最大的答案。在本网站上,点赞或接受的答案都算作“感谢”。

标签: .net arrays vb.net


【解决方案1】:

您对数组的分配是错误的。这个:

etudianttemp() = tableautemp(i).Split(";"c)

应该是:

etudianttemp = tableautemp(i).Split(";"c)

您得到的错误是因为看起来您尝试分配给数组中的一个项目而不是数组本身,然后您需要提供该项目的索引。错误信息只是基于左边赋值错误,没有考虑到右边。

【讨论】:

    【解决方案2】:

    删除etudianttemp() = ... => etudianttemp = ... 中的括号,如Guffa's answer 中所述(不要忘记将他的答案标记为已接受


    我将补充一点,您应该在尝试使用 @987654325 在循环中分配值之前初始化您的 tableau 数组大小 @ 和 j。顺便说一句,如果您删除了代码中的某些行以本地化您的问题,请丢弃此答案:)。

        Dim tableautemp() As String = IO.File.ReadAllLines(nomfichier)
        Dim etudianttemp() As String
        Dim tailleDimensionJ As Int32 = 6 ' edit accordingly.. Caution: Base 0 => 7 items
    
        Redim tableau(tailleDimensionJ, tableautemp.length - 1) ' here !
    
        For i As Integer = 0 To tableautemp.Length - 1
            etudianttemp = tableautemp(i).Split(";"c)
            For j As Integer = 0 To tailleDimensionJ ' and here !
                tableau(j, i) = etudianttemp(j)
            Next
        Next
    

    如果没有固定大小的 J 维度,则应在运行时通过首先解析每个 tableautemp(i) 来设置 tailleDimensionJ,只保留最大项目数。

        Dim tableautemp() As String = IO.File.ReadAllLines(nomfichier)
        Dim etudianttemp() As String
        Dim tailleDimensionJ As Int32 = 0
    
        For i As Integer = 0 To tableautemp.Length - 1
            etudianttemp = tableautemp(i).Split(";"c)
            If tailleDimensionJ < (etudianttemp.Length - 1)
                tailleDimensionJ = etudianttemp.Length - 1
            End If
        Next
    
        Redim tableau(tailleDimensionJ, tableautemp.length - 1)
    
        For i As Integer = 0 To tableautemp.Length - 1
            etudianttemp = tableautemp(i).Split(";"c)
            For j As Integer = 0 To etudianttemp.Length - 1 ' <- change this
                tableau(j, i) = etudianttemp(j)
            Next
        Next
    

    [FR] Vous devriez initialiser la taille de votre variable tableau avant de lui assigner des valeurs dans la boucle for à l'aide de i et j。 Bien entendu, si vous aviez supprimé des lignes de code pour bien cibler le soucis, veuillez ignorer cette remarque :)

    【讨论】:

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