-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilterList.java
More file actions
69 lines (61 loc) · 1.91 KB
/
FilterList.java
File metadata and controls
69 lines (61 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
public class FilterList {
// Write a function named "evens" that takes as input a
// list of Integer (almost but not quite int) and returns
// a new list of ints containing only the even elements
// of the input.
public static List<Integer> evens(List<Integer> input) {
// Here are some reminders:
//
// You can find input's length using input.size().
//
// You can find the remainder of a division using %. For instance,
// 2 == 11%3 and 1 == 25 % 4.
//
// You can declare a new list named "clown" of length n with the
// syntax:
//
// List<Integer> clown = new ArrayList<Integer>(10);
// STUDENTS, WRITE CODE HERE.
List<Integer> evenNums = new ArrayList<Integer>();
for (Integer i : input)
if (i % 2 == 0)
evenNums.add(i);
return evenNums;
}
public static void main(String[] args) {
List<Integer> test1 = new ArrayList<Integer>(Arrays.asList(8, 6, 7, 5,
3, 0, 9));
List<Integer> ans = evens(test1);
// Expected output: 8, 6, 0
for (Integer n : ans) {
System.out.print(Integer.valueOf(n) + ", ");
}
System.out.println();
List<Integer> test2 = new ArrayList<Integer>(Arrays.asList(2, 7, 18,
28, 18, 28, 45, 90, 45));
ans = evens(test2);
// Expected output: 2, 18, 28, 18, 28, 90
for (Integer n : ans) {
System.out.print(Integer.valueOf(n) + ", ");
}
System.out.println();
// STUDENTS: ADD YOUR TEST CASES HERE.
List<Integer> test3 = new ArrayList<Integer>(Arrays.asList(99, 99, 813, 223));
ans = evens(test3);
// Expected output:
for (Integer n : ans) {
System.out.print(Integer.valueOf(n) + ", ");
}
System.out.println();
List<Integer> test4 = new ArrayList<Integer>(Arrays.asList(2, 45, 90, 45));
ans = evens(test4);
// Expected output: 2, 18, 28, 18, 28, 90
for (Integer n : ans) {
System.out.print(Integer.valueOf(n) + ", ");
}
System.out.println();
}
}