Click here to Skip to main content
15,794,850 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello;
I have this List
VB
Dim Lst as New List(of Array)
Lst.Add({12,100,58})
Lst.Add({25,47,23})
Lst.Add({15,19,203})

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)
Posted

VB.NET
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))
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.
 
Share this answer
 
Comments
Member 9451601 15-Jan-14 14:15pm    
I noticed you wrote List(of Integer()) Instead of List(of Array())
But you added an array and not an integer
So can you explain
Meshack Musundi 15-Jan-14 14:48pm    
The arrays you are adding to your list are arrays of Integers so it's a List(Of Integer()) (List of Integer arrays). Note the opening and closing brace after Of Integer.
Try this:
VB
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)

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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900