Java 传统循环和 for-each 循环有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33582050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is a difference between traditional loop and for-each loop?
提问by shanks
I wonder if there is a difference between these:
我想知道这些之间是否有区别:
ArrayList<Example> list = new ArrayList<Example>
1-)
1-)
for(int i = 0; i < list.size(); i++) {
list.get(i).doSomething();
}
2-)
2-)
for(Example example : list) {
example.doSomething();
}
If there is not any difference which one is more common or efficient?
如果没有任何区别,哪个更常见或更有效?
采纳答案by Jaroslaw Pawlak
Traditional loop
传统循环
for (int i = 0; i < list.size(); i++) {
list.get(i).doSomething();
}
- allows to modify the list, e.g.:
- you can add extra element at the end of list and it will be also iterated through
- you know the index
- can be used to refer to another list of the same size
- can be used to refer to previous/next element
- efficient only in
RandomAccess
lists- in case of
LinkedList
in every iteration of the loop,get(i)
will have to iterate over all elements starting fromhead/tail
toi
- in case of
- works only with
List
sinceList#get(int)
is used - error prone, a lot of things that can go wrong, e.g.:
i = 0;
instead ofint i = 0;
- will refer to variable declared before the loop, possible side effects outside of the loop>
instead of<
- loop will not executej++
instead ofi++
- infinite loop.get(j)
instead of.get(i)
- will always get the same element
- 允许修改列表,例如:
- 你可以在列表的末尾添加额外的元素,它也会被迭代
- 你知道指数
- 可用于引用另一个相同大小的列表
- 可用于引用上一个/下一个元素
- 仅在
RandomAccess
列表中 有效- 如果
LinkedList
在循环的每次迭代中,get(i)
将不得不迭代所有元素,从head/tail
到i
- 如果
- 仅适用于
List
因为List#get(int)
使用 - 容易出错,很多事情都可能出错,例如:
i = 0;
而不是int i = 0;
- 将引用在循环之前声明的变量,循环外可能的副作用>
而不是<
- 循环不会执行j++
而不是i++
- 无限循环.get(j)
而不是.get(i)
- 将始终获得相同的元素
For-each loop
For-each 循环
for (Example example : list) {
example.doSomething();
}
- does not allow to modify the list
- trying to do so will most likely result in
ConcurrentModificationException
- trying to do so will most likely result in
- you don't know the index of the element
- you cannot refer to previous/next element
- efficient in all cases because uses
Iterator
specific for the collection- efficient in case of
LinkedList
- efficient in case of
- works not only with every
Collection
, but with everyIterable
sinceIterable#iterator()
is used- you can easily replace
List
with aSet
- no changes to the loop required - you can easily replace with your own class, it just has to implement
Iterable
- you can easily replace
- more robust (less code, fewer special characters)
- 不允许修改列表
- 尝试这样做很可能会导致
ConcurrentModificationException
- 尝试这样做很可能会导致
- 你不知道元素的索引
- 你不能引用上一个/下一个元素
- 在所有情况下都有效,因为
Iterator
特定于集合的 用途- 有效的情况下
LinkedList
- 有效的情况下
- 不仅适用于 every
Collection
,而且适用于 everyIterable
sinceIterable#iterator()
被使用- 您可以轻松替换
List
为Set
- 无需更改循环 - 你可以很容易地用你自己的类替换,它只需要实现
Iterable
- 您可以轻松替换
- 更健壮(更少的代码,更少的特殊字符)
Summary
概括
for-each
loop wins with a score 3 : 2.
for-each
Loop 以 3 : 2 的比分获胜。
The only reason to use a traditional loop is when:
使用传统循环的唯一原因是:
- the index of element is required, or
- the list has to be modified
- 元素的索引是必需的,或
- 列表必须修改
回答by Sebastian S
回答by Andrew Tobilko
for(:)
statement is a read-only loop. You cannot change a collection within this loop. Also you cannot use more than one element.
for(:)
语句是一个只读循环。您不能在此循环中更改集合。此外,您不能使用多个元素。
The traditional for
statement doesn't have such limits.
传统的for
语句没有这样的限制。
回答by Mohammed Aouf Zouag
They are basically the same, but for-each (the second one) has certain restrictions.
它们基本相同,但是 for-each(第二个)有一定的限制。
It can be used for accessingthe array elements but not for modifyingthem.
It is not usablefor loops that must iterate over multiple collections in parallel—for example, to compare the elements of two arrays.
It can be used only for a single element access and cannot be used to compare successive elements in an array. It is a forward-onlyiterator. If you want to access only a few elements of the array, you would need to use the traditional for loop.
它可用于访问数组元素,但不能用于修改它们。
它不适用于必须并行迭代多个集合的循环——例如,比较两个数组的元素。
它只能用于单个元素访问,不能用于比较数组中的连续元素。它是一个只进迭代器。如果只想访问数组的几个元素,则需要使用传统的 for 循环。