+ 3

For-each

How I use for-each loop?

17th Nov 2016, 10:31 PM
Martin Martin
Martin Martin - avatar
3 Respostas
+ 3
public static void main(String[] args){ int[] array = {2, 5, 9, 3, 4}; for(int x: array){ System.out.println(x); } } The output will be: 2 5 9 3 4 The variable x is taking the values in the array.
18th Nov 2016, 12:04 AM
Ran
Ran - avatar
+ 3
Hi martin ! With java 8 specifications , java has added a new programming style of loop within Lists so just you have to check if ur JDK is grater or equal to 1.8 to use the below code : public class ForEach{ public static void main(String [] args) { List<Integer> array = new ArrayList<>(); array.add(1); array.add(2); array.add(3); array.forEach(element -> { System.out.println(element); }); }} // Output: 1 2 3
18th Nov 2016, 7:42 PM
yassine essefi
yassine essefi - avatar
- 3
int v for(a=0;a<v;a++)
18th Nov 2016, 8:16 AM
Morteza
Morteza - avatar