如何搜索数组列表? [英] How to search a List of array?
问题描述
你好;
我有这个清单
Hello;
I have this List
Dim Lst as New List(of Array)
Lst.Add({12,100,58})
Lst.Add({25,47,23})
Lst.Add({15,19,203})
我可以在此列表中搜索确切的内容并获取索引使用循环。
例如搜索47,他将找到Lst(1)
Can I search this list for an exact and get the index without using a loop.
For example search for 47, he will get found Lst(1)
推荐答案
Dim Lst As New List(Of Integer())
Lst.Add({12, 100, 58})
Lst.Add({25, 47, 23})
Lst.Add({15, 19, 203})
Dim index = Lst.FindIndex(Function(n) n.Contains(47))
注意 List
是一个列表(Integer())
。这使您可以在Lambda表达式中调用 Contains()
方法,该表达式是 FindNext()
方法的谓词。
Note that the List
is a List(of Integer())
. This enables you to call the Contains()
method in the Lambda expression that is the predicate of the FindNext()
method.
试试这个:
Try this:
Dim Lst As New List(Of Array)
Lst.Add({12, 100, 58})
Lst.Add({25, 47, 23})
Lst.Add({15, 19, 203})
Dim arrIndex As Integer = -1
Dim i As Integer = 0
For Each arr As Integer() In Lst
If arr.Contains(47) Then
arrIndex = i
Exit For
End If
i += 1
Next
Console.WriteLine(arrIndex)
这将打印 1
。如果你搜索一个不存在的整数,它将打印 -1
如何工作:迭代 List
中的每个数组,如果该数组包含 47
,则 arrIndex
变量将被设置为列表中数组的索引。
This will print 1
. If you search for an integer that doesn't exist, it will print -1
How this works: you iterate over each array in the List
, and if that array contains 47
, the arrIndex
variable will be set to the index of the array in the list.
这篇关于如何搜索数组列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!