LinkedList 应该使用什么样的迭代?

发布于 2024-12-06 06:40:18 字数 512 浏览 1 评论 0原文

我知道通过索引遍历 LinkedList 是不好的,因为 list.get(n) 是在线性时间 O(n) 内执行的。因此我不应该使用索引。我查看了由 iterator() 调用返回的 AbstactList.Itr,它也使用 get(cursor) 。我很困惑。

正如@axtavt所指出的,LinkedList.iterator()实际上调用了listIterator(),它返回AbstactList.ListItr,它扩展了AbstactList。 Itr 并且它只添加了 ListIterator 接口的实现。因此,获取下一个元素的机制仍然与 AbstactList.Itr 中的相同。 `AbstactList.Itrnext(); 在指定列表上调用 get(n)

I know that traversing LinkedList by indexing is bad, because list.get(n) is executed in linear time O(n). Thus i shouldn't use indexing. I looked at the AbstactList.Itr that is returned by iterator() call, it uses get(cursor) too. I'm confused.

As pointed out by @axtavt , LinkedList.iterator() actually calls listIterator() which returns AbstactList.ListItr which extends AbstactList.Itr and it only adds implementations of ListIterator interface. Thus the machinery for getting next element is still the same as in AbstactList.Itr. `AbstactList.Itr's next(); calls get(n) on the specified list.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

巷子口的你 2024-12-13 06:40:18

foreach 循环非常高效且简单:

List<String> list = new LinkedList<String>();
list.add("First");
list.add("Second");
list.add("Third");
list.add("Fourth");

for(String s : list) {
    System.out.println(s);
}

The foreach loop is very efficient, and easy:

List<String> list = new LinkedList<String>();
list.add("First");
list.add("Second");
list.add("Third");
list.add("Fourth");

for(String s : list) {
    System.out.println(s);
}
梦魇绽荼蘼 2024-12-13 06:40:18

any List(实际上甚至any Iterable)的最佳迭代是 对于每个循环:

for(Element e : myList){
    // do something
}

The best iteration over any List (actually even any Iterable) is the for each loop:

for(Element e : myList){
    // do something
}
画中仙 2024-12-13 06:40:18

LinkedList 不仅继承自 AbstractList,还继承自 AbstractSequentialList,后者又实现了 iterator(),如下所示:

public Iterator<E> iterator() {
    return listIterator();
}

以及 ListIterator LinkedList 返回的 code> 很聪明地使用顺序访问。

因此,无论您使用 foreach、iterator() 还是 listIterator(),您始终处理相同的智能迭代器类型。

LinkedList inherits not only from AbstractList, but also from AbstractSequentialList which in turn implements iterator() like this:

public Iterator<E> iterator() {
    return listIterator();
}

and the ListIterator returned by LinkedList is smart about using sequential access.

So whether you use foreach, iterator() or listIterator(), you are always dealing with the same smart iterator type.

红尘作伴 2024-12-13 06:40:18

如果可以的话,您应该使用 foreach 循环 - 这是最有效的:

List<Item> myList = ...;

for (Item item : myList) {
  ...
}

如果您需要修改循环内的列表,或者在一个循环中遍历多个集合,请使用迭代器:

List<Item> myList = ...;
Iterator<Item> it = myList.iterator();

while (it.hasNext()) {
  Item item = it.next();
  ...
}

如果您需要向后遍历,或者其他什么特定于链表,使用listIterator

ListIterator<Item> it = myList.listIterator();
...

You should use a foreach loop if you can - that's the most efficient:

List<Item> myList = ...;

for (Item item : myList) {
  ...
}

If you need to modify the list inside the loop, or traverse through multiple collections in one loop, use an iterator:

List<Item> myList = ...;
Iterator<Item> it = myList.iterator();

while (it.hasNext()) {
  Item item = it.next();
  ...
}

And if you need to traverse backwards, or something else specific to linked lists, use listIterator:

ListIterator<Item> it = myList.listIterator();
...
旧人哭 2024-12-13 06:40:18

您可以使用迭代器,它由 iterator 方法返回。转到下一项不需要 O(n) 时间。它使用 LinkedListListIterator 实现,因此速度很快。

You can use iterator, which returned by iterator method. It doesn't require O(n) time to go to next item. It uses ListIterator implementation from LinkedList so it's fast.

美人骨 2024-12-13 06:40:18

您可以使用Iterator,它可以让您更快地访问。列表的示例代码:

 List<String> list = new LinkedList<String>();
    list.add("First");
    list.add("Second");
    list.add("Third");
    list.add("Fourth");

    Iterator it = list.iterator();

    while(it.hasNext())
    {
        System.out.println(it.next());
    }

You can use the Iterator, it gives you a faster access. A sample code for a list:

 List<String> list = new LinkedList<String>();
    list.add("First");
    list.add("Second");
    list.add("Third");
    list.add("Fourth");

    Iterator it = list.iterator();

    while(it.hasNext())
    {
        System.out.println(it.next());
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文