ch 3java
ch 3java
Repetition Statements
– As of Java 5, the enhanced for loop was introduced. This is mainly used for Arrays
– Syntax
– Declaration: The newly declared block variable, which is of a type compatible with
the elements of the array you are accessing. The variable will be available within the
for block and its value would be the same as the current array element.
– Expression: This evaluates to the array you need to loop through. The expression
can be an array variable or method call that returns an array.
OOP Prepared By: Daniel Tesfy 36
Enhanced For Loop example
– add(E e): The add(E e) method is used to add the specified element e in
the ArrayList.
– add(int index, E e): The add(int index, E e) method is used to add the
specified element at a specific position in the ArrayList.
– addAll(Collection c): The addAll(Collection c) is used to add the
collection of elements to the end of the ArrayList.
– addAll(int index, Collection c): The addAll(int index, Collection c) is
used to add the collection of elements at specified position in the
ArrayList. OOP Prepared By: Daniel Tesfy 55
ArrayListTest