Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

":" in the for() function

Tags:

java

I'm doing an assignment that involves a board. The base code is given for us to modify, but I don't understand what the : in the parameters of the for() means. Does it go through all the board (the ArrayList)?

private ArrayList<MovingElement> moveElems = new ArrayList<MovingElement>();

for (MovingElement mElement : moveElems) {
    mElement.step();
}
like image 814
Big Puncho Avatar asked Nov 30 '22 05:11

Big Puncho


2 Answers

This is a special form of the for loop used to iterate over arrays and any Iterable, which includes any Collection.

This is referred to as a for-each loop, as in: for each element of a list.

Read: for (MovingElement mElement : moveElems) as _for each MovingElement in the collection moveElems_.

See: The For-Each Loop.

like image 127
pb2q Avatar answered Dec 10 '22 05:12

pb2q


This is for-each loop in Java.

For each element in Arraylist (or) array.

The element will be assigned to MovingElement mElement which is scoped to the for loop .

like image 41
kosa Avatar answered Dec 10 '22 06:12

kosa