Never ever use
ArrayList
. This class was rendered obsolete as early as of .NET v.2.0, with introduction of generics. If is not formally marked as obsolete only because it is good enough to leave it in some legacy code. Its potential problem is a need for type casts, an extra source of possible bugs (why do you thing generics were introduced for?).
Instead, use the generic class
System.Collections.Generic.List<>
:
http://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.110%29.aspx[
^].
For search, you can use the methods named
Find
or
FindIndex
, using a
predicate parameter, so you can search by any search criteria:
http://msdn.microsoft.com/en-us/library/0k601hd9%28v=vs.110%29.aspx[
^],
http://msdn.microsoft.com/en-us/library/x0b5b5bc%28v=vs.110%29.aspx[
^].
The MSDN documentation referenced above is crystal clear, so I hope you can solve your problem with search easily.
Naturally, the
time complexity of such search is O(N) (linear). You can have complexity of O(1) (asymptotically, not depending on the size of data), with collections based on
backets with indexing by some key (which you would need to create based on your search criteria):
http://msdn.microsoft.com/en-us/library/xfhwa508%28v=vs.110%29.aspx[
^],
http://msdn.microsoft.com/en-us/library/xfhwa508%28v=vs.110%29.aspx[
^],
http://msdn.microsoft.com/en-us/library/ms132319%28v=vs.110%29.aspx[
^].
Please see:
http://en.wikipedia.org/wiki/Big_O_notation[
^].
I'm not explaining the part related to key-indexed collection because this is not a part of the question, but you better learn these types, as well as all the collections, as knowing them is very important for your work.
—SA