没有任何重复值的数组

时间:2015-11-10 11:31:48

标签: vb.net

生成否的代码。一个数组中的数组正在运行。我试着对它进行一些更改,如下所示

Function myarray(ByVal arra1() As Integer, ByVal arran() As Integer, ByVal arrNumber As Integer) As Integer()

    arran = arra1.Clone()
    For i As Integer = 0 To arra1.Length - 1
        If i = (arrNumber - 1) Then ' IF arrNumber is 1 then +1 to index 0, If it is 2 then +1 to index 1
            arran(i) = arra1(i) + 1
            'If there are two duplicate value make on of them zero at a time
            For k = 0 To arran.Length - 1
                For j = k + 1 To arran.Length - 1
                    If arran(k) = arran(j) Then
                        arran(k) = 0
                    End If
                    'make any value great than 11 zero
                    If arran(i) > 11 Then
                        arran(i) = 0
                    End If
                Next
            Next
        Else
            arran(i) = arra1(i)
        End If
    Next
    'Print the array
    For i = 0 To arran.Length - 1
        Console.Write(arran(i) & " ")

    Next

    Console.WriteLine()
    Return arran

End Function   

我真正需要的是将例如{1,4,5,5}分解为{1,4,0​​,5}然后{1,4,5,0}以上代码仅生成{1 ,4,0,5}

1 个答案:

答案 0 :(得分:0)

我没有测试过这个,但我相信下面的代码会做你想要的。根据您的评论,我已将函数更改为将所有结果数组作为数组数组返回,而不是要求索引更改为输入并返回一个数组。我也忽略了0的匹配,因为你描述的条件似乎不是为了处理它们而设计的。由于它的递归,我认为这种方法将成功处理诸如{3, 3, 3, 3}

之类的输入
Public Function jaggedArray(ByVal inputArray() As Integer) As Integer()()
    If inputArray Is Nothing Then
        Return Nothing
    Else
        Dim resultArrays()(), i, j As Integer
        Dim arrayMax As Integer = inputArray.GetUpperBound(0)
        If arrayMax = 0 Then    'prevents errors later if only one number passed
            ReDim resultArrays(0)
            If inputArray(0) > 11 Then
                resultArrays(0) = {1}
            ElseIf inputArray(0) = 11 Then
                resultArrays(0) = {0}
            Else
                resultArrays(0) = {inputArray(0) + 1}
            End If
            Return resultArrays
        End If
        For i = 0 To arrayMax
            Dim tempArray() As Integer = inputArray.Clone
            For j = 0 To arrayMax
                If tempArray(j) > 11 Then
                    tempArray(j) = 0
                End If
            Next
            If tempArray(i) = 11 Then
                tempArray(i) = 0
            Else
                tempArray(i) += 1
            End If
            splitArray(resultArrays, tempArray)
        Next
        Return resultArrays
    End If
End Function
Private Sub splitArray(ByRef arrayList()() As Integer, ByVal sourceArray() As Integer)
    Dim x, y As Integer 'positions of matching numbers
    If isValid(sourceArray, x, y) Then
        If arrayList Is Nothing Then
            ReDim arrayList(0)
        Else
            ReDim Preserve arrayList(arrayList.Length)
        End If
        arrayList(arrayList.GetUpperBound(0)) = sourceArray
    Else
        Dim xArray(), yArray() As Integer
        xArray = sourceArray.Clone
        xArray(x) = 0
        splitArray(arrayList, xArray)
        yArray = sourceArray.Clone
        yArray(y) = 0
        splitArray(arrayList, yArray)
    End If
End Sub
Private Function isValid(ByRef testArray() As Integer, ByRef match1 As Integer, ByRef match2 As Integer) As Boolean
    For i As Integer = 0 To testArray.GetUpperBound(0) - 1
        If testArray(i) > 11 Then
            testArray(i) = 0
        End If
        For j As Integer = i + 1 To testArray.GetUpperBound(0)
            If testArray(j) > 11 Then
                testArray(j) = 0
            End If
            If testArray(i) = testArray(j) AndAlso testArray(i) > 0 Then    'added second test to prevent infinite recursion
                match1 = i
                match2 = j
                Return False
            End If
        Next
    Next
    match1 = -1
    match2 = -1
    Return True
End Function