본문 바로가기

카테고리 없음

제네릭

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
package stack;
 
class GStack<T> {
    int tos;
    Object[] stck;
    public GStack() {
        tos = 0;
        stck = new Object[10];
    }
    
    public void push (T item) {
        if(tos == 10) { 
            return;
        }
        stck[tos] = item;
        tos++;
    }
    public T pop() {
        if(tos == 0) {
            return null;
        }
        tos--;
        return (T)stck[tos];
    }
    
}
 
public class Stack {
 
    public static void main(String[] args) {
        GStack<String> stringStack = new GStack<String>();
        stringStack.push("seoul");
        stringStack.push("busan");
        stringStack.push("LA");
 
        for (int n = 0; n<3; n++) {
            System.out.println(stringStack.pop());
        }
        
        GStack<Integer> intStack = new GStack<Integer>();
        intStack.push(1);
        intStack.push(2);
        intStack.push(3);
        
        
        for(int n = 0; n<3; n++) {
            System.out.println(intStack.pop());
        }
    
    }
}
 
cs