473,385 Members | 1,983 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

ArrayList and indexOf

Hello!

Assume I have created several object of a class called Test and then used
add all these Test object to an ArrayList.

Assume also that in this class Test I have a field with a property called
name.
Assume I add a Test object where the name field is equal to "Nisse"

Now to my question:
Is it possible to find if there exist an object in the ArrayList with the
name="Nisse"

//Tony
Aug 23 '06 #1
2 6973
tony wrote:
Hello!

Assume I have created several object of a class called Test and then used
add all these Test object to an ArrayList.

Assume also that in this class Test I have a field with a property called
name.
Assume I add a Test object where the name field is equal to "Nisse"

Now to my question:
Is it possible to find if there exist an object in the ArrayList with the
name="Nisse"
Yes - write a loop and check each object.

foreach (Test t in list)
if (t.Name == "Nisse")
// found

If you used List<Tinstead of ArrayList (and you're using C# 2.0 on
..NET 2.0), you could do it with a predicate passed to List<T>.FindAll()
/ List<T>.FindIndex(), but it wouldn't really be any less code.

If you were using the LINQ preview of C# 3.0, you could write:

bool found = list.FindIndex(e =e.Name == "Nisse") != -1;

.... but that's not for production use yet. :)

-- Barry

--
http://barrkel.blogspot.com/
Aug 23 '06 #2
Tony,

If the ordering of elements is not important to you you can use Hastable
instead. Hastables registers elements under some key value. You can use
latter on the key to extract the element.

Hashtable ht = new Hashtable()

ht[test.name] = test;

ht["Nisse"] will return the object which proeprty name was Nisse.

Keep in mind that keys cannot duplicate as well as if you change the *name*
property after the object has been added to the hashtable trying to retrieve
the item using the new name won't work. The element needs to be re-added
upon changing the name.
--
HTH
Stoitcho Goutsev (100)
"tony" <jo*****************@telia.comwrote in message
news:Oo**************@TK2MSFTNGP06.phx.gbl...
Hello!

Assume I have created several object of a class called Test and then used
add all these Test object to an ArrayList.

Assume also that in this class Test I have a field with a property called
name.
Assume I add a Test object where the name field is equal to "Nisse"

Now to my question:
Is it possible to find if there exist an object in the ArrayList with the
name="Nisse"

//Tony


Aug 23 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dan V. | last post by:
I would like to create a 2D string list (2D ArrayList ???). I would like to pass in a table or query as a parameter and have both columns transform into a 2D ArrayList. When I sort the one...
9
by: vbportal | last post by:
Hi, I would like to add BitArrays to an ArrayList and then remove any duplicates - can someone please help me forward. I seem to have (at leaset ;-) )2 problems/lack of understanding (see test...
12
by: Rubbrecht Philippe | last post by:
Hi there, According to documentation I read the ArrayList.IndexOf method uses the Object.Equals method to loop through the items in its list and locate the first index of an item that returns...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
10
by: JohnR | last post by:
I have an arraylist of string values. I would like to search the arraylist to find the index of a particular string and I would like the search to be case insensitive. dim al as new arraylist...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.