Junior — Middle
What actions need to be performed to apply the For Each construct in a loop?
sobes.tech AI
Answer from AI
To use the For Each construct in Java, you need:
- To have a collection or array to iterate over.
- To use the syntax:
for (Type element : collection) {
// actions with the element
}
For example:
int[] numbers = {1, 2, 3, 4};
for (int num : numbers) {
System.out.println(num);
}
This allows you to iterate over all elements of the array without needing to use an index.