有人可以建议,我会为以下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
您的子例程声明将evenarray和oddarray声明为2维数组(,) - 但是,当您在它们上调用新的时,您将它们重新定义为单个维数阵列()将它们视为锯齿状阵列。假设您希望它们是多维数组,则需要在调用新的阵列时给它们两个数组边界,并始终用两个索引(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
上面的实际上是在分配它们,就好像它们是锯齿状阵列一样,就像多维阵列一样,但不一样(一个就像网格,另一个像条形图一样,如果锯齿状的数组为您的视觉表示形式)您希望他们通常按照以下方式更有声明。
evenArray = New Integer(rows -1)()
,然后像这样访问:
evenArray(row)(column) = value
差异很简单,多维数组实际上是网格,所有行都具有相同数量的列。在锯齿状的数组中,您的所有行都可能具有不同数量的列,并且每行需要您调用新的列或分配单个维数阵列(这是您的循环正在执行的操作)
https://msdn.microsoft.com/en-us/library/hkhhsz9t(v = vs.90).aspx
希望有帮助。:)