180k views
4 votes
The enhanced for statement allows you to iterate through the elements of an array or a collection without using a counter. Give an example. Notice: The enhanced for statement cannot be used to modify elements in an array. If a program needs to modify elements, use the traditional counter-controlled for statement.

2 Answers

5 votes

Answer:

See explaination

Step-by-step explanation:

public class EnhancedForExample

{

public static void main(String[] args) {

int[] arr = {3, 5, 9, 4, 5, 9};

for(int num : arr) {

System.out.println(num);

}

}

User Taseer
by
6.3k points
1 vote

Answer:

Check the explanation

Step-by-step explanation:

public class EnhancedForExample {

public static void main(String[] args) {

int[] arr = {4, 2, 9, 1, 5, 7};

for(int num : arr) {

System.out.println(num);

}

}

}

Kindly check the attached image below for the code output.

The enhanced for statement allows you to iterate through the elements of an array-example-1
User Parag Bafna
by
4.9k points