Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a difference between traditional loop and for-each loop? [duplicate]

Tags:

java

I wonder if there is a difference between these:

ArrayList<Example> list = new ArrayList<Example>

1-)

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

2-)

for(Example example : list) {
    example.doSomething();
}

If there is not any difference which one is more common or efficient?

like image 512
shanks Avatar asked Nov 07 '15 11:11

shanks


People also ask

What is a traditional for loop?

The traditional for loop in Apex corresponds to the traditional syntax used in Java and other languages. Its syntax is: for (init_stmt; exit_condition; increment_stmt) { code_block }

Which is an advantage of a traditional for loop rather than an enhanced for loop?

The advantage of the for-each loop is that it eliminates the possibility of bugs and makes the code more readable. It is known as the for-each loop because it traverses each element one by one. The drawback of the enhanced for loop is that it cannot traverse the elements in reverse order.


Video Answer


3 Answers

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 from head/tail to i
  • works only with List since List#get(int) is used
  • error prone, a lot of things that can go wrong, e.g.:
    • i = 0; instead of int i = 0; - will refer to variable declared before the loop, possible side effects outside of the loop
    • > instead of < - loop will not execute
    • j++ instead of i++ - infinite loop
    • .get(j) instead of .get(i) - will always get the same element

For-each loop

for (Example example : list) {
    example.doSomething();
}
  • does not allow to modify the list
    • trying to do so will most likely result in ConcurrentModificationException
  • 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
  • works not only with every Collection, but with every Iterable since Iterable#iterator() is used
    • you can easily replace List with a Set - no changes to the loop required
    • you can easily replace with your own class, it just has to implement Iterable
  • more robust (less code, fewer special characters)

Summary

for-each loop wins with a score 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
like image 167
Jaroslaw Pawlak Avatar answered Oct 26 '22 18:10

Jaroslaw Pawlak


They are basically the same, but for-each (the second one) has certain restrictions.

  1. It can be used for accessing the array elements but not for modifying them.

  2. It is not usable for loops that must iterate over multiple collections in parallel—for example, to compare the elements of two arrays.

  3. 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-only iterator. If you want to access only a few elements of the array, you would need to use the traditional for loop.

like image 27
Mohammed Aouf Zouag Avatar answered Oct 26 '22 18:10

Mohammed Aouf Zouag


The second one works with every type of (potentially unordered) Iterable, as it doesn't rely on random access, i.e. get(i).

like image 26
Sebastian S Avatar answered Oct 26 '22 16:10

Sebastian S