-
Notifications
You must be signed in to change notification settings - Fork 0
/
collection_class.java
37 lines (31 loc) · 1001 Bytes
/
collection_class.java
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
package collections;
import java.util.Scanner;
import java.util.Iterator;
import java.util.Collection;
import java.util.ArrayList;
public class collection_class {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// String Collection
Collection<String> myCollection = new ArrayList<String>();
myCollection.add("Phil");
myCollection.add("Mary");
myCollection.add("Betty");
myCollection.add("Bob");
printAll(myCollection);
// Integer Collection
Collection<Integer> intCollection = new ArrayList<Integer>();
intCollection.add(12);
intCollection.add(56);
intCollection.add(23);
intCollection.add(76);
printAll(intCollection);
scan.close();
}
static <T> void printAll(Collection<T> coll) {
Iterator<T> iter = coll.iterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
}
}