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

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
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
 
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
 
Back
Top