首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

在LinkedList中为何迭代比索引访问List更快

2012-12-28 
在LinkedList中为什么迭代比索引访问List更快Why would iterating over a List be faster than indexing t

在LinkedList中为什么迭代比索引访问List更快
Why would iterating over a List be faster than indexing through it?

在linked list中,所有的元素都指向下一个元素



如果要访问item3,你要从head开始直到item3,因为你不能直接指向item3

所以,如果我们打印所有元素的值,如果我们这样写



这样性能就非常差,因为每次都要重头开始

如果使用


这样性能就提升了很多

如果换做ArrayList就不会出现这种问题,因为它是以array实现的

热点排行