-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolymorphismEx3.java
More file actions
38 lines (30 loc) · 981 Bytes
/
Copy pathPolymorphismEx3.java
File metadata and controls
38 lines (30 loc) · 981 Bytes
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
package com.study.polymorphism;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class PolymorphismEx3 {
static void addIntegersToList(List<Integer> list, int count) {
for(int i=0; i < count; ++i) list.add(i);
}
static void removeOddNumbers(List<Integer> list) {
Iterator<Integer> iterator = list.iterator();
while(iterator.hasNext()) {
int i = iterator.next();
if(i % 2 == 1) iterator.remove();
}
}
static void printList(List<Integer> list) {
for(int i : list) System.out.printf("%d ", i);
System.out.println();
}
static void doSomething(List<Integer> list) {
addIntegersToList(list, 20);
removeOddNumbers(list);
printList(list);
}
public static void main(String[] args) {
doSomething(new ArrayList<Integer>());
doSomething(new LinkedList<Integer>());
}
}