-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGenericsCast.java
More file actions
45 lines (33 loc) · 963 Bytes
/
GenericsCast.java
File metadata and controls
45 lines (33 loc) · 963 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
39
40
41
42
43
44
45
package generics;
import java.util.*;
import java.lang.reflect.*;
/**
* RUN:
* javac generics/GenericsCast.java && java generics.GenericsCast
* OUTPUT:
*
*/
public class GenericsCast {
public static final int SIZE = 10;
public static void main(String[] args) {
FixedSizeStack<String> strings = new FixedSizeStack<String>(SIZE);
for (String s : "A B C D E F G H I J".split(" ")) {
strings.push(s);
// System.out.print(s + " ");
}
for (int i = 0; i < SIZE; i++) {
String s = strings.pop();
System.out.print(s + " ");
}
}
}
class FixedSizeStack<T> {
private int index = 0;
private Object[] storage;
public FixedSizeStack(int size) {
storage = new Object[size];
}
public void push(T item) { storage[index++] = item; }
@SuppressWarnings("unchecked")
public T pop() { return (T)storage[--index]; }
}