Main.java
//Note: This class will not compile yet.
import java.io.*;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.ArrayList;
public class Main {
private List<Integer> list;
private static final int SIZE = 20;
public Main () {
list = new ArrayList<>(SIZE);
for (int i = 0; i < SIZE; i++) {
list.add(i);
}
// Predicate<Integer> findEvens = new Predicate<Integer>() {
//
// @Override
// public boolean test(Integer t) {
// return (t % 2 == 0);
// }
// };
//list.removeIf(findEvens);
//list.removeIf(t -> t%2==0);
List<Integer> ivan = list.stream().filter(t->t%2==0).collect(Collectors.toList());
for (var i : ivan) {
System.out.println("Ivan says: " + i);
}
}
public void writeList() {
// The FileWriter constructor throws IOException, which must be caught.
try (PrintWriter out = new PrintWriter(new FileWriter("OutFile.txt"))) {
//PrintWriter out = new PrintWriter(new FileWriter("OutFile.txt"));
for (int i = 0; i < list.size(); i++) {
// The get(int) method throws IndexOutOfBoundsException, which must be caught.
out.println("Value at: " + i + " = " + list.get(i));
}
//out.close();
} catch (IOException e) {
System.out.println("Oh, look! an IOException just got thrown.");
} catch (IndexOutOfBoundsException e) {
System.out.println("Oops. Index out of bounds. Fix that.");
}
}
public static void main(String[] a) {
Main foo = new Main();
foo.writeList();
}
}