有人可以请教,我收到以下vb.net代码的编译错误。
VS突出显示几个错误。
Public Sub Int_SplitEvenOdd_2D_Array(ByRef rawArray(,) As Integer, ByRef evenArray(,) As Integer, ByRef oddArray(,) As Integer)
Dim firstUpperBound As Integer = rawArray.Length
Dim secondUpperBound As Integer = rawArray(0).Length
Dim rows As Integer = 3
Dim columns As Integer = 2
evenArray = New Integer((rows) - 1) {}
oddArray = New Integer((rows) - 1) {}
Dim i As Integer = 0
Do While (i <= columns)
evenArray(i) = New Integer((columns) - 1) {}
oddArray(i) = New Integer((columns) - 1) {}
i = (i + 1)
Loop
Dim value As Integer = 0
Dim EvenIndex As Integer = 0
Dim EvenIndex2 As Integer = 0
Dim OddIndex As Integer = 0
Dim OddIndex2 As Integer = 0
Do While (i < firstUpperBound)
Dim j As Integer = 0
Do While (j < secondUpperBound)
value = rawArray(i)(j)
If ((value Mod 2) = 0) Then
evenArray(EvenIndex)(EvenIndex2) = (value + 2000)
EvenIndex2 = (EvenIndex2 + 1)
If (EvenIndex2 = columns) Then
EvenIndex2 = 0
EvenIndex = (EvenIndex + 1)
End If
Else
oddArray(OddIndex)(OddIndex2) = (value + 3000)
OddIndex2 = (OddIndex2 + 1)
If (OddIndex2 = columns) Then
OddIndex2 = 0
OddIndex = (OddIndex + 1)
End If
End If
j = (j + 1)
Loop
i = (i + 1)
Loop
End Sub
答案 0 :(得分:1)
你的子程序声明将evenArray和oddArray声明为二维数组(,) - 但是当你在它们上面调用New时,你将它们重新定义为单维数组() - 然后你将它们视为锯齿状数组。假设您希望它们是多维数组,您需要在调用New时为它们提供两个数组边界,并始终使用两个索引(x,y)引用它们。
evenArray = New Integer(rows - 1, columns -1) {}
oddArray = New Integer(rows - 1, columns -1) {}
https://msdn.microsoft.com/en-us/library/d2de1t93(v=vs.90).aspx
正确的声明将不再需要以下内容,您可以将它们视为锯齿状数组:
Do While (i <= columns) 'Also note: This should be the number of rows, not columns if you want to use jagged arrays.
evenArray(i) = New Integer((columns) - 1) {}
oddArray(i) = New Integer((columns) - 1) {}
i = (i + 1)
Loop
上面实际上是将它们分配为Jagged数组,就像多维数组一样但是不一样(一个像Grid一样,另一个像条形图给你一个可视化表示)如果Jagged数组是什么你希望他们通常有更多的声明:
evenArray = New Integer(rows -1)()
并按以下方式访问:
evenArray(row)(column) = value
区别很简单,多维数组实际上是一个Grid,所有行都有相同的列数。在一个锯齿状数组中,所有行都可以有不同数量的列,每行需要您调用New或为其分配一个维度数组(这就是你的for循环所做的)
https://msdn.microsoft.com/en-us/library/hkhhsz9t(v=vs.90).aspx
希望有所帮助。 :)