138k views
0 votes
4. What is for…each loop in java? Explain with example.

User Debugger
by
3.2k points

1 Answer

5 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 Jossefaz
by
2.9k points