在多维数组中查找多个字符串的索引

时间:2013-12-30 13:39:41

标签: arrays vb.net

我有一个包含多个字符串的二维数组。它看起来像这样;

[1] [2] [3]  
[1] [4] [5]  
[7] [9] [8]

我需要一段代码来返回数组中所有位置[1]的索引。

3 个答案:

答案 0 :(得分:1)

这是对tinstaafl代码的改进答案

Sub CountryCode(ByVal searchterm As String)
    Dim data As New List(Of List(Of String))(
        {
            {"AF", "Afghanistan"}.ToList,
            {"AL", "Albania"}.ToList,
            {"DZ", "Algeria"}.ToList,
            {"AS", "American Samoa"}.ToList,
            {"AD", "Andorra"}.ToList,
            {"AO", "Angola"}.ToList,
            {"AI", "Anguilla"}.ToList,
            {"AQ", "Antarctica"}.ToList,
            {"AG", "Antigua And Barbuda"}.ToList,
            {"AR", "Argentina"}.ToList,
            {"AM", "Armenia"}.ToList,
            {"AW", "Aruba"}.ToList,
            {"AU", "Australia"}.ToList,
            {"AT", "Austria"}.ToList,
            {"AZ", "Azerbaijan"}.ToList
        })

    Dim coords As New List(Of Tuple(Of Integer, Integer))
    For Row As Integer = 0 To data.Count - 1
        Dim found As Integer = data(Row).IndexOf(searchterm)
        If found >= 0 Then
            Debug.Print(Row, found)
            coords.Add(New Tuple(Of Integer, Integer)(Row, found))
            'Based on the index number of the item and the second piece will be displayed
            '{"AF", "Afghanistan"}.ToList,      'index: 1       "AF" is item 1 and "Afghanistan" is item 2
            '{"AL", "Albania"}.ToList,          'index: 2       "AL" is item 1 and "Albania" is item 2
            '{"DZ", "Algeria"}.ToList,          'index: 3       
            '{"AS", "American Samoa"}.ToList,   'index: 4
            '{"AD", "Andorra"}.ToList,          'index: 5
            Debug.Print(data(Row).Item(1).ToString) 'prints anything in the right column... searchterm = "AT" would print "Austria"
        End If
    Next
End Sub

答案 1 :(得分:0)

以下是您的代码示例:

Dim a(,) As String = {{"[1]", "[2]", "[3]"},
                      {"[1]", "[4]", "[5]"},
                      {"[7]", "[9]", "[8]"}}

Dim indexList As New List(Of Tuple(Of Integer, Integer))
For iRow As Integer = 0 To UBound(a, 1)
  For jCol As Integer = 0 To UBound(a, 2)
    If a(iRow, jCol) = "[1]" Then 'save i and j somewhere
      indexList.Add(New Tuple(Of Integer, Integer)(iRow, jCol))
    End If
  Next
Next

在执行结束时,indexList有两个值(0,0)(1,0),其中第一个索引是一行,第二个索引是一列。您可能希望将其更改为使用自定义结构而不是Tuple。从长远来看,泛型有点难以调试,因为调试器中出现了奇怪的名称。

答案 2 :(得分:0)

如果您的数据包含许多元素,您可能不希望迭代每个元素。使用List(Of List(Of String))将允许您仅循环遍历外部列表并只检查内部列表。像这样:+

    Dim data As New List(Of List(Of String))(
        {
            {"[1]", "[2]", "[3]"}.ToList,
            {"[1]", "[4]", "[5]"}.ToList,
            {"[7]", "[9]", "[8]"}.ToList
        })
    Dim searchterm As String = "[1]"
    Dim coords As New List(Of Tuple(Of Integer, Integer))
    For Row As Integer = 0 To data.Count - 1
        Dim found As Integer = data(Row).IndexOf(searchterm)
        If found >= 0 Then
            coords.Add(New Tuple(Of Integer, Integer)(Row, found))
        End If
    Next