vb.net 索引数小于索引数组的维数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26832783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:27:00  来源:igfitidea点击:

Number of indices is less than the number of dimensions of the indexed array

.netarraysvb.net

提问by Jawad322

    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

I want to read the file and put the lines in a 1d tab, then split each line in another 1d tab then add everything to a 2d tab. But I get "Number of indices is less than the number of dimensions of the indexed array". I don't understand :s

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

回答by Guffa

Your assignment of the array is wrong. This:

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

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

should be:

应该:

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

The error that you got is because it looks like you try to assign to an item in the array rather than the array itself, and then you would need to provide the index of the item. The error message is only based on the fact that the left side of the assignment is wrong, it doesn't take into account the right side.

您遇到的错误是因为您似乎尝试分配给数组中的一个项目而不是数组本身,然后您需要提供该项目的索引。错误信息仅基于赋值左侧错误这一事实,并未考虑右侧。

回答by Karl Stephen

Remove the parenthesis in etudianttemp() = ...=> etudianttemp = ...as explained in Guffa's answer(don't forget to mark his answer as accepted)

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



I'll just add that you shouldinitialize your tableauarray size beforeattempting to assign values in the loop using iand j. By the way, if you've deleted some lines in your code to localize your issue, just discard this answer :) .

我只想补充一点,尝试使用and在循环中分配值之前您应该初始化tableau数组大小。顺便说一句,如果您删除了代码中的一些行来本地化您的问题,只需放弃这个答案:)。ij

    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

If you don't have a fixed size J dimension, you should set tailleDimensionJ at runtime by parsing each tableautemp(i) first, only keeping the maximum number of items.

如果您没有固定大小的 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 tableauavantde lui assigner des valeurs dans la boucle for à l'aide de iet j. Bien entendu, si vous aviez supprimé des lignes de code pour bien cibler le soucis, veuillez ignorer cette remarque :)

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