how to use foreach in java code example
Example 1: foreach java
for (String name : names) {
System.out.println(name);
}
Example 2: java foreach
List<String> someList = new ArrayList<String>();
for (String item : someList) {
System.out.println(item);
}
Example 3: java foreach method
names.forEach(name -> {
System.out.println(name);
});
Example 4: for-each loop in java
import java.util.*;
public class ForEachLoopExample
{
public static void main(String[] args)
{
int[] numbers = {2, 4, 6, 8, 10};
for(int n : numbers)
{
System.out.println(n);
}
}
}
Example 5: how to do for each in java
for(int i : alist)
{
i+=1
}