Answered by : thankful-teira-rjuvzt7ui3c1
int [] intArray = { 10, 20, 30, 40, 50 };
for( int value : intArray ) { System.out.println( value );
}
Source : | Last Update : Mon, 20 Apr 20
Answered by : polytrope
//itemset contains variables of type <Data Type>
for(<Data Type> item : itemset) { //loop
}
Source : | Last Update : Wed, 18 Dec 19
Answered by : syed-nayeem-ridwan
// Before anything, I have overriden Object toString() method inside Vehicle
@Override
public String toString() { System.out.println("Name = " + getName()); System.out.println("Speed = " + getSpeed()); return "--------------";
}
// Method 1 : Printing the normal way
Vehicle vehicleList[] = new Vehicle[] { stationWagon1, sportsCar, train };
for (Vehicle vehicle : vehicleList) { System.out.println(vehicle); }
// Method 2 : Printing with lambda expression
List<Vehicle> vehicleList2 = Arrays.asList(stationWagon1, sportsCar, train);
vehicleList2.forEach(vehicle -> System.out.println(vehicle));
// Method 3 : Printing with method referece
vehicleList2.forEach(System.out::println);
Source : | Last Update : Tue, 19 Dec 23
Answered by : bright-beaver-8nod75kaf7kz
List<String> someList = new ArrayList<String>();
// add "monkey", "donkey", "skeleton key" to someList
for (String item : someList) { System.out.println(item);
}
Source : https://stackoverflow.com/questions/85190/how-does-the-java-for-each-loop-work | Last Update : Sun, 05 Apr 20
Answered by : itchy-ibis-kpltqfbsttz5
for (String name : names) { System.out.println(name);
}
Source : | Last Update : Tue, 07 Apr 20
Answered by : alina-samatova
for (String item : someList) { System.out.println(item);
}
Source : https://stackoverflow.com/questions/85190/how-does-the-java-for-each-loop-work | Last Update : Mon, 21 Mar 22
Answered by : oussama-saidi
List<String> list = List.of("a", "b", "c");
list.forEach(elt -> System.out.println(elt)) /* output: a b c */
Source : | Last Update : Wed, 16 Nov 22
Answered by : grotesque-gazelle-47a0tlit1tix
names.forEach(name -> { System.out.println(name);
});
Source : https://www.baeldung.com/foreach-java | Last Update : Fri, 30 Apr 21
Answered by : jittery-jay-8bkoc05j9gc4
int value;
int[] arr = {6, 7, 8};
for (value : arr) { System.out.print(val + " ");
}
Source : | Last Update : Sun, 22 May 22