迭代器vs [英] Iterator vs for

查看:117
本文介绍了迭代器vs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在采访中被问到使用迭代器使用for循环的优点是什么,或者使用循环迭代器的优点是什么?



可以请任何人回答这个问题,以便将来如果我面对类似的问题,那么我可以回答这个问题。首先,有两种类型的问题: for循环,其行为非常不同。一个使用索引:

pre $ for(int i = 0; i< list.size(); i ++){
物件t = list.get(i);
...
}

这种循环并不总是可能的。例如,列表有索引,但是集合不是,因为它们是无序的集合。另一个,foreach循环在幕后使用Iterator:

/ p>

  for(Thing thing:list){
...
}
$ b $ p
$ b $ p

这对每一种Iterable集合(或数组) (Iterator< Thing> it:list.iterator);最后,你可以使用一个迭代器,它也适用于任何Iterable:

  (); it.hasNext();){
Thing t = it.next();
...



$ b $ p

所以你实际上有3个循环来比较。


您可以用不同的术语来比较它们:性能,可读性,易错性,性能。

一个迭代器可以执行一个foreach循环不能做的事情。例如,如果迭代器支持迭代器,则可以删除元素:

  for(Iterator< Thing> it :list.iterator(); it.hasNext();){
Thing t = it.next();
if(shouldBeDeleted(thing){
it.remove();
}
}



列表还提供了可以在两个方向上迭代的迭代器,foreach循环只是从开始到结束迭代。

迭代器更危险,可读性更差。当需要使用foreach循环时,它是最可读的解决方案。使用迭代器,可以执行以下操作,这将是一个错误:

  for(Iterator< Thing> it:list.iterator(); it.hasNext();){
System.out.println(it.next() .getFoo());
System.out.println(it.next()。getBar());
}

foreach循环不允许发生这样的错误。

使用索引访问元素的效率稍高但是如果你改变主意并使用LinkedList而不是ArrayList,那么突然间的性能将变得糟糕,因为每次访问 list.get(i),链表将不得不循环所有的元素,直到第i个元素。一个迭代器(也就是foreach循环)没有这个问题。它总是使用最好的方式遍历给定集合的元素,因为集合本身有它自己的迭代器实现。



我的一般经验法则是:使用foreach循环,除非你真的需要一个迭代器的能力。当我需要访问循环内的索引时,我只会使用带数组索引的循环。


I was asked in an interview what is the advantage of using iterator using for loop or what is the advantage of using for loop over iterator?

Can any body please answer this so that in future If I face similar question then I can answer that

解决方案

First of all, there are 2 kinds of for loops, which behave very differently. One uses indices:

for (int i = 0; i < list.size(); i++) {
    Thing t = list.get(i);
    ...
}

This kind of loop isn't always possible. For example, Lists have indices, but Sets don't, because they're unordered collections.

The other one, the foreach loop uses an Iterator behind the scenes:

for (Thing thing : list) {
    ...
}

This works with every kind of Iterable collection (or array)

And finally, you can use an Iterator, which also works with any Iterable:

for (Iterator<Thing> it: list.iterator(); it.hasNext(); ) {
    Thing t = it.next();
    ...
} 

So you in fact have 3 loops to compare.

You can compare them in different terms: performance, readability, error-proneness, capability.

An Iterator can do things that a foreach loop can't. For example, you can remove elements while you're iterating, if the iterator supports it:

for (Iterator<Thing> it: list.iterator(); it.hasNext(); ) {
    Thing t = it.next();
    if (shouldBeDeleted(thing) {
        it.remove();
    }
} 

Lists also offer iterators that can iterate in both directions. A foreach loop only iterates from the beginning to an end.

But an Iterator is more dangerous and less readable. When a foreach loop is all you need, it's the most readable solution. With an iterator, you could do the following, which would be a bug:

for (Iterator<Thing> it: list.iterator(); it.hasNext(); ) {
    System.out.println(it.next().getFoo());
    System.out.println(it.next().getBar());
} 

A foreach loop doesn't allow for such a bug to happen.

Using indices to access elements is slightly more efficient with collections backed by an array. But if you change your mind and use a LinkedList instead of an ArrayList, suddenly the performance will be awful, because each time you access list.get(i), the linked list will have to loop though all its elements until the ith one. An Iterator (and thus the foreach loop) doesn't have this problem. It always uses the best possible way to iterate through elements of the given collection, because the collection itself has its own Iterator implementation.

My general rule of thumb is: use the foreach loop, unless you really need capabilities of an Iterator. I would only use for loop with indices with arrays, when I need access to the index inside the loop.

这篇关于迭代器vs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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