Tim the Enchanter
Tim the Enchanter

Reputation: 11127

Search arraylist of objects

I have an arraylist of objects, I would like to know the index within the arraylist of the object that contains a certain value, is there a built-in way to do the search?

I know I could simply iterate through the arraylist to find the correct value e.g. :

ReportToFind="6"

For i = 0 To ReportObjList.Count - 1
    If ReportObjList.Item(i).ReportCode = ReportToFind Then
        ReportName.Text = ReportObjList.Item(i).ReportName ' found it - show name
        Exit For
    End If
Next

Is the only other solution be to replace this code a binary search?

.Net 1.1

Upvotes: 0

Views: 2747

Answers (3)

Beth
Beth

Reputation: 9617

It looks like you need to index your reportObjectList by reportCode in addition to the item index. You can do this either in a second parallel list with the reportCode as the index and the itemIndex as the value.

Upvotes: 0

ajh1138
ajh1138

Reputation: 566

I don't know if .Net 1.1 has it, but you could try the .IndexOf method on your array list.

Upvotes: 0

Markus Koivisto
Markus Koivisto

Reputation: 609

You need to use better data structures in the case that searching through a list is a problem. You can use a binary search for your arraylist in the case that your list is sorted with respect to the value to be searched. In other cases you would be better of using smarter data structures such as a binary tree or a map.

Upvotes: 1

Related Questions