为什么我们不能在arraylist中使用index Of()和contains()方法 [英] why can't we use index Of() and contains() methods in arraylist

查看:181
本文介绍了为什么我们不能在arraylist中使用index Of()和contains()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
你能给我一个为什么我们不能在arraylist中使用index Of()和contains()方法的正当理由吗?


谢谢您,

Hi all,
Can u please give me valid reasons for why can''t we use index Of() and contains() methods in arraylist.


Thanking you,

推荐答案

谁告诉您我们不能?我们可以,但这不是世界上最快的方法.对于相对较小的收藏,这是完全可以的.这完全取决于您对性能的要求.另外,应谨慎使用.请参阅: http://www.daniweb.com/software-development/java/threads/109506 [ ^ ].

关于算法的速度,它期望它具有O(n)的复杂度,即线性的. (有关此表示法,请参见 http://en.wikipedia.org/wiki/Big_O_notation [
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


这篇关于为什么我们不能在arraylist中使用index Of()和contains()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆