447,906 views
5 votes
5 votes
4. What is for…each loop in java? Explain with example.

User Hosseio
by
3.2k points

1 Answer

21 votes
21 votes

Answer:

In Java, the for-each loop is used to iterate through elements of arrays and collections (like ArrayList)

Step-by-step explanation:

The syntax of the Java for-each loop is:

for(dataType item : array) {

...

}

Here,

array - an array or a collection

item - each item of array/collection is assigned to this variable

dataType - the data type of the array/collection

User Bill Greer
by
3.3k points