搜索数组数组中的索引

时间:2012-09-07 22:03:19

标签: arrays vb.net search

我有一个数组数组:

Dim IDMUsers As List(Of Array)

我用这些信息填充这个数组。例: [{Data1a,Data2a,Data3a,...,Data30a},{Data1b,Data2b,Data3b,...,Data30b},{DATA1C,DATA2C,DATA3C,...,Data30c}]

现在,我需要找到例如“Data3b”并且该函数必须返回它所在的寄存器号,在这种情况下它必须返回记录号2.即{Data1b,Data2b,Data3b,....,Data30b }

我在VB.Net工作。

我的代码是:

Sub Main()
    Dim TecoReader As New FileManagement
    Dim IDMUsers As List(Of Array)

   ' Populate Array
    IDMUsers = TecoReader.ReadFromFile(My.Settings.ActivosIDMFilePath, My.Settings.CharSeparator)

   Console.writeline (SearchInArray("text"))
end sub


Private Function SearchInArray(ByVal ToSearchFor As String) As String
    For Each Register In IDMUsers
        For Each Field In Register
            If Field = ToSearchFor Then
                Return Register(26) & " - " & Register(27)
            End If
        Next
    Next
    Return ""
End Function

它适用于SeachInArray函数,但我有一个大型数组(20K寄存器),所以非常慢,我需要一个更好的方法。

1 个答案:

答案 0 :(得分:0)

我的代码在C#中,您可以避免至少一个 FOR EACH 循环....

int mainArrayIndex = 0;
        string arrayIndex = string.Empty;
        int searchIdex = 0;
        foreach (string[] item in jaggedArray)
        {
            mainArrayIndex++;
            searchIdex = Array.BinarySearch(item, "H");
            if (searchIdex > 0)
            {
                arrayIndex = mainArrayIndex + "," + searchIdex.ToString();
                break;
            }
        }