Competence: I can use the enhanced for-loop to iterate over list elements.
Specific learning goals:
while
loop to iterate over an ArrayList by index and retrieve elements.while
loop uses an index as a counter to access elements by position.enhanced for-loop
simplifies iterating over an ArrayList without using an explicit index.enhanced for-loop
and can iterate over various lists.enhanced for-loop
automatically iterates through the entire list, hiding the index or position of elements.The assignments can be found in the separate modules.
If you want to perform the same operation on all elements in an ArrayList, a loop is essential. We show here two ways that are useful for an ArrayList.
An important property of the ArrayList is that it can grow and shrink during usage, however this means that if you want to create a loop this has to be based on the actual amount of elements in the list. This can be achieved with the while-loop and the enhanced for-loop.
The while loop uses an index to iterate over the elements in the list. By using this index items can be retrieved from the list based on their position. Using this index and the number of elements in the list, it is easy to create a condition that will properly end before the list runs out of elements.
The enhanced for-loop simplifies this process at the cost of a little freedom. Elements are automatically linked to some kind of predefined variable allowing for easy access, but the programmer no longer has access to the index (or position) or the element and is unable to manipulate the list (so adding / removing elements is not allowed). The enhanced for-loop therefore should only be used to view the contents of lists or perform other read-based operations (like searching).
The while loop is the simplest way of iterating over any list. Using the counter as an index variable and by get
ting elements from certain positions, it is rather easy to navige through a list.
Suppose we have an ArrayList
1. int index = 0;
2. while (index <names.size ()) {
3. SaxionApp.printLine (names.get (index));
4. index ++
5. }
Below is a brief summary of what is happening in this code:
line 1 initializes the variable index with 0.
Iteration 1:
Iteration 2:
Iteration 3:
End of loop.
With the enhanced for loop, the entire list is run through without the programmer having to define the stop point. This comes at the cost of not knowing exactly where you are in the list (so the position is hidden) and you are unable to manipulate the list in any way (so no additions / removal of items is allowed).
The enhanced for-loop is designed to be a “simpler” way of accessing all elements in a list.
We have another ArrayList
1. for (String singleName: names) {
2. SaxionApp.printLine (singleName);
3. }
The for statement is structured as follows:
(You should read this line as for all singleNames in the list of names do..)
Below is a brief summary of what is happening in this code:
Iteration 1:
Iteration 2:
There is no further iteration as there were only two elements in the list!