Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi all,
Can u please give me valid reasons for why can't we use index Of() and contains() methods in arraylist.


Thanking you,
Posted
Comments
Sergey Alexandrovich Kryukov 14-Oct-11 13:41pm    
Why do you think we can't?
--SA
V V S GANGA SUJAN 14-Oct-11 14:35pm    
Good answer. Thanks alot
Sergey Alexandrovich Kryukov 15-Oct-11 19:51pm    
I you think so, please accept it formally (green button) -- thanks.
--SA

1 solution

Who told you we can't? We can, only this is not the fastest method in the World. For relatively small collections it's perfectly fine. It all depends on your requirements to performance. Also, you should use it with care. See: http://www.daniweb.com/software-development/java/threads/109506[^].

As to the speed of the algorithm, it expect it have the complexity of O(n), that is, linear. (On this notation, see http://en.wikipedia.org/wiki/Big_O_notation[^].) A hash table or a dictionary based on hash values and buckets will give you O(1) — approximating a constant time of execution on big collection, that is, with a big collections the time does not depends on the size of collection. So, IndexOf is only good for small collections or when you want to save on memory and the search time is still acceptable.

—SA
 
Share this answer
 
v2
Comments
Simon Bang Terkildsen 15-Oct-11 18:11pm    
my 5
Sergey Alexandrovich Kryukov 15-Oct-11 19:50pm    
Thank you, Simon.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900