How to search for a specific name is a collection of student objects in a ArrayList

T

Tony Johansson

Hello!

I know I can use Generics and Linq but in this test example I just want to
use an ArrayList.
I create some student which have fields like name,adress course age and so
on.
These students is added to the ArrayList collection.
If I now want to search for a specific student by using the name then the
only way that I know is to
enumerate through the collection and check if the name matches ?

My question is if there exist a better solution. Note that I must use
ArrayList

//Tony
 
S

Suhu

Hello!

I know I can use Generics and Linq but in this test example I just want to
use an ArrayList.
I create some student which have fields like name,adress course age and so
on.
These students is added to the ArrayList collection.
If I now want to search for a specific student by using the name then the
only way that I know is to
enumerate through the collection and check if the name matches ?

My question is if there exist a better solution. Note that I must use
ArrayList

//Tony

You can use the BinarySearch functionality in the ArrayList. The only
caveat is your student class has to implement the IComparable
interface.

Thanks
Suhu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top