Loops
package com.minte9.basics.loops;
import java.util.Arrays;
import java.util.List;
public class Loops {
public static void main(String[] args) {
System.out.print("\nFor loop: ");
for (int i=0; i<3; i++) {
System.out.print(i);
}
System.out.print("\nEnhanced for: ");
int[] nums = {0, 1, 2};
for (int no : nums) {
System.out.print(no);
}
System.out.print("\nforEach with lambdas: ");
List<Integer> A = Arrays.asList(0, 1, 2);
A.forEach(x -> System.out.print(x));
System.out.print("\nforEach with method reference: ");
List<Integer> B = Arrays.asList(0, 1, 2);
B.forEach(System.out::print);
}
}
Breaks
package com.minte9.basics.loops;
public class Breaks {
public static void main(String[] args) {
int[][] numbers = {
{1,2,3},
{4,5,6},
{7,8,9},
};
A: for (int i=0; i<numbers.length; i++) {
B: for (int j=0; j<numbers[i].length; j++) {
System.out.print(i + ":" + j + " ");
if (numbers[i][j] == 4) {
System.out.println("\nBreak from B");
break B;
}
if (numbers[i][j] == 8) {
System.out.println("\nBreak from A");
break A;
}
}
}
}
}