从2D数组中删除行

时间:2016-03-23 13:01:17

标签: arrays vb.net

我在堆栈溢出中找到了下面的代码,这段代码是从2D数组中删除行,并且它在c#中成功运行,我的问题是将其转换为vb.net,当我转换它时它不起作用。任何帮助,请

 Public Function TrimArray(ByVal rowToRemove As Integer, ByVal originalArray As Integer(,)) As Integer(,)

 Dim result As Integer(,) = New Integer(originalArray.GetLength(0) - 2, originalArray.GetLength(1) - 1) {}

    Dim i As Integer = 0, j As Integer = 0
    While i < originalArray.GetLength(0)
        If i = rowToRemove Then
            Continue While
        End If
        Dim p As Integer = 0, m As Integer = 0
        While p < originalArray.GetLength(1)
            result(j, m) = originalArray(i, p)
            m += 1
            p += 1
        End While
        j += 1
        i += 1
    End While

    Return result
End Function

2 个答案:

答案 0 :(得分:0)

这应该有效:

Public Shared Function DeleteRow(rowToRemove As Integer, Array1 As Integer(,)) As Integer(,)
    Dim Array2 As Integer(,) = New Integer(Array1.GetLength(0) - 2, Array1.GetLength(1) - 1) {}
    Dim i As Integer = 0, j As Integer = 0
    While i < Array1.GetLength(0)
        If i = rowToRemove Then
            Continue While
        End If
        Dim k As Integer = 0, m As Integer = 0
        While k < Array1.GetLength(1)
            Array2(j, m) = Array1(i, k)
            m += 1
            k += 1
        End While
        j += 1
        i += 1
    End While
    Return Array2
End Function

好吧,我自己没有转换它。你也不需要这样做。你可以只使用代码转换器:

  

Code Converter | Provided by Telerik

答案 1 :(得分:0)

代码正在运行,感谢所有

Public Function DeleteRow(ByVal rowToRemove As Integer, ByVal Array1 As Integer(,)) As Integer(,)
    Dim Array2 As Integer(,) = New Integer(Array1.GetLength(0) - 2, Array1.GetLength(1) - 1) {}
    Dim j As Integer = 0
    For i = 0 To Array1.GetLength(0) - 1

        If i = rowToRemove Then

            Continue For

        End If

        Dim k As Integer = 0, m As Integer = 0
        While k < Array1.GetLength(1)
            Array2(j, m) = Array1(i, k)
            m += 1
            k += 1
        End While

        j += 1
    Next

    Return Array2
End Function