将2D数组与1D数组进行比较

时间:2015-12-20 13:25:22

标签: arrays vb.net

如果我的2d数组像{{3,7,9},{1y,8,6},如果在2D数组中找不到,则如何将每行2D数组与1d数组进行比较以插入1D数组我和我的1d数组例如{3,7,9},我怎么知道它是否已经存在,我的代码下面一次插入1d数组到2d数组,我试图使用.equal但它不起作用

    Private Sub Add_Item_Array_2D2(ByRef Array_2D As Double(,), ByVal d As Integer,
                        ByVal Items As Double())
    Dim tmp_array(Array_2D.GetUpperBound(0) + d, Array_2D.GetUpperBound(d)) As Double

    For n As Integer = 0 To Array_2D.GetUpperBound(0)
        For m = 0 To Array_2D.GetUpperBound(1)
            Dim pp = (Array_2D(n, m)).Equals(Items)
            If pp = False Then

                For x As Integer = 0 To Array_2D.GetUpperBound(0)
                    For k = 0 To Array_2D.GetUpperBound(1)

                        tmp_array(x, k) = Array_2D(x, k)
                    Next
                Next
                For j = 0 To Items.Count - 1


                    tmp_array(tmp_array.GetUpperBound(0), j) = Items(j)

                Next
                Array_2D = tmp_array


            End If

        Next
    Next

End Sub

1 个答案:

答案 0 :(得分:1)

如何确定2D数组中的行是否等于1D数组:

Compare bounds. If the column count of your 2D array is unequal 
to the length of your 1D array, throw an error.

Loop through the rows of the 2D array:
    If, for all column indexes, 2D(row, column) = 1D(column):
        return Success

return Failure

将此算法转换为VB.NET是一项练习。提示:对于for all部分,Enumerable.RangeEnumerable.All可能会有所帮助。