":" in the for () function

I am doing an assignment that includes a board. The basic code is provided for us to change, but I do not understand what it means : in the for () parameters. Does it go through all the boards ( ArrayList )?

 private ArrayList<MovingElement> moveElems = new ArrayList<MovingElement>(); for (MovingElement mElement : moveElems) { mElement.step(); } 
+4
source share
8 answers

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

This is called a for-each loop, as in: for each list item.

Consider: for (MovingElement mElement : moveElems) as _ for each MovingElement in the moveElems_ collection.

See: For each cycle .

+10
source

This is a for-each loop in Java.

For each element of the Arraylist (or) array.

The item will be assigned to the MovingElement mElement that is bound to the for loop.

+4
source

Think of it this way:

 for (MovingElement mElement : moveElems) { // translates into English like this: // for EACH MovingElement object in the ArrayList moveElems, // do the following code: mElement.step(); } 

This is for every cycle.

+2
source

This is a simple foreach loop, it will go through each of the elements in this array.

In this case, the MovingElement will be an array type, mElement will be the current element, and moveElems will be the actual array.

":" divides only two parts

So, in your case, the loop will go through each of the elements in the ArrayList and use the step () method.

+2
source

This is called foreach or advanced for looping in Java. ':' char separates the type / name of the iteration variable from the object to be moved.

Here is a little tutorial explaining its use, it is useful to know that it was introduced in version 1.5 of the Java language. It is syntactic sugar, the same iteration behavior can be achieved using the standard for loop and / or using iterators.

The foreach loop can be used to iterate over elements in an array or over an object (usually with a collection) that implements Iterable .

For reference, the foreach statement is defined in section ยง14.14.2 of the Java language specification.

+1
source

This is Java for every syntax. It is more or less equivalent:

 Iterator<MovingElement> iter = moveElems.iterator(); while (iter.hasNext()) { iter.next().step(); } 
+1
source

Each loop is intended to simplify the most common form of iteration, where an iterator or index is used solely for iteration, and not for any other operations.

http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html

0
source

- What you just met is known as extended for the cycle or better known as for each cycle.

for( DataType variable : Iterable )

For instance:

for( String variable : arr )

arr - May be an array or collections (i.e. Iterables), of type String

s - will take each arr value at each iteration and assign it s

0
source

Source: https://habr.com/ru/post/1437871/


All Articles