Finding 1 object index in an arraylist

  • Thread starter Thread starter Dan Smith
  • Start date Start date
D

Dan Smith

I have an arraylist that contains a large amount of objects. I'm having trouble figuring out how to find the index of 1 object based upon matching data from outside the arraylist to data inside the arraylist object. Indexof only gives me a -1 but not the actual index. Any ideas?

Thanks

Submitted via EggHeadCafe - Software Developer Portal of Choice
Silverlight 4 Martin Fractals with WriteableBitmapEx
http://www.eggheadcafe.com/tutorial...4-martin-fractals-with-writeablebitmapex.aspx
 
This should go, but be aware that the arraylist is already an old beast from
vb language version 7.
From the start of 8 are the generic collections used (which are in fact
based on the arraylist but then strongly typed)

Dim ar As New ArrayList From {"First", "Second", "Third"}'version 10 style
Dim y = ar.IndexOf("Third")

In a generic list it would be almost the same
Dim arl As New List(Of String) From {"First", "Second", "Third"} 'version 10
style
Dim z = ar.IndexOf("Third")

The benefit of the generic lists is that in a list a string is direct usable
while in an arraylist (which are objects) you have every time to cast the
items to strings.

Success

Cor

"Dan Smith" wrote in message
I have an arraylist that contains a large amount of objects. I'm having
trouble figuring out how to find the index of 1 object based upon matching
data from outside the arraylist to data inside the arraylist object. Indexof
only gives me a -1 but not the actual index. Any ideas?

Thanks

Submitted via EggHeadCafe - Software Developer Portal of Choice
Silverlight 4 Martin Fractals with WriteableBitmapEx
http://www.eggheadcafe.com/tutorial...4-martin-fractals-with-writeablebitmapex.aspx
 
Cor,

Thanks for the response. Unfortunately The objects stored in the arraylist are all dynamically placed and I don't know how many objects are going to be added each time the program runs.

What I unfortunately had to do was create a loop and track the loop count until I found what I wanted:

Dim pholding As New ArrayList
Dim persons As person
Dim i as integer
Dim j as integer

For Each person In pholding
i+= 1
If persons.name = input.text then
j = i
end if
Next persons

Is there any cleaner way of doing this?

Submitted via EggHeadCafe - Software Developer Portal of Choice
SharePoint Create List Add/Edit Form Web Part With Custom Toolbar and Attachments Option
http://www.eggheadcafe.com/tutorial...th-custom-toolbar-and-attachments-option.aspx
 
Am 11.10.2010 21:36, schrieb Dan Smith:
Cor,

Thanks for the response. Unfortunately The objects stored in the arraylist are all dynamically placed and I don't know how many objects are going to be added each time the program runs.

What I unfortunately had to do was create a loop and track the loop count until I found what I wanted:

Dim pholding As New ArrayList
Dim persons As person
Dim i as integer
Dim j as integer

For Each person In pholding
i+= 1
If persons.name = input.text then
j = i
end if
Next persons

Is there any cleaner way of doing this?

Not with the ArrayList. As Cor said, depending on the language version,
you can use a generic list:

Dim pholding As New List(Of person)
Dim index As Integer
'...
index = pholding.FindIndex(Function(person) person.name = input.text)
 
With the generic List I would use the code from Armin.

However, to teach you an easier way with non so named strongly typed
objects.

Dim i As Integer
For i = 0 To pholding.Count - 1
If DirectCast(pholding(i), Person).name = input.text Then
Exit For
End If
Next


"Dan Smith" wrote in message
Cor,

Thanks for the response. Unfortunately The objects stored in the arraylist
are all dynamically placed and I don't know how many objects are going to be
added each time the program runs.

What I unfortunately had to do was create a loop and track the loop count
until I found what I wanted:

Dim pholding As New ArrayList
Dim persons As person
Dim i as integer
Dim j as integer

For Each person In pholding
i+= 1
If persons.name = input.text then
j = i
end if
Next persons

Is there any cleaner way of doing this?

Submitted via EggHeadCafe - Software Developer Portal of Choice
SharePoint Create List Add/Edit Form Web Part With Custom Toolbar and
Attachments Option
http://www.eggheadcafe.com/tutorial...th-custom-toolbar-and-attachments-option.aspx
 
Armin,

I always had the idea that the Arraylist was a kind of base from the generic
list (I did not even investigate it, but it is not)
However I took your code and was trying also the new From in version VB10 a
little bit deeper, here the sample,
you see no continuation characters in this code, not forgotten,
they are for this code not needed anymore.

Module Module1
Sub Main()
Dim pholding As New List(Of Person) From
{New Person With {.name = "armin"},
New Person With {.name = "dan"},
New Person With {.name = "cor"}}
Console.Write(pholding.FindIndex(Function(person) person.name =
"dan"))
Console.ReadLine()
End Sub
End Module
Public Class Person
Public Property name As String
End Class

:-)

Cor

"Armin Zingler" wrote in message
Am 11.10.2010 21:36, schrieb Dan Smith:
Cor,

Thanks for the response. Unfortunately The objects stored in the arraylist
are all dynamically placed and I don't know how many objects are going to
be added each time the program runs.

What I unfortunately had to do was create a loop and track the loop count
until I found what I wanted:

Dim pholding As New ArrayList
Dim persons As person
Dim i as integer
Dim j as integer

For Each person In pholding
i+= 1
If persons.name = input.text then
j = i
end if
Next persons

Is there any cleaner way of doing this?

Not with the ArrayList. As Cor said, depending on the language version,
you can use a generic list:

Dim pholding As New List(Of person)
Dim index As Integer
'...
index = pholding.FindIndex(Function(person) person.name = input.text)
 
Back
Top