SKILL/JAVA

자바 제네릭 삽입, 삭제, 검색...직접 구현

Jedy_Kim 2017. 12. 20. 21:46
728x90
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import java.util.Scanner;
  
class Logic {     
    Object[] buffer;
    int maxIdx;
    int curIdx; 
    
    public Logic(){}
    public Logic(int maxIdx) {
        this.maxIdx = maxIdx;
        buffer = new Object[maxIdx];
        curIdx = 0;         
    } 
    public boolean add(Object msg) {        
        if(curIdx == buffer.length) {            
            System.out.println(++maxIdx);
            Object[] tempBuf = new Object[maxIdx];    
            System.arraycopy(buffer, 0, tempBuf, 0, buffer.length);                  
            tempBuf[curIdx++= msg;
            buffer = tempBuf;            
            return true;
        } else {
            buffer[curIdx] = msg;
            curIdx++;
            return true;
        }         
    }
    public void selectAll(){         
        for(int i = 0; i < buffer.length; i++) {
            System.out.println(i+" : "+buffer[i]);
        }
    }
    public void selectOne(Object obj){         
        for(int i = 0; i < buffer.length; i++) {            
            if(buffer[i].equals(obj) == true) {
                System.out.println(buffer[i] + "의 인덱스 값은 " + i);
            }
        }
    }
    public void delOne(Object obj) {
        for(int i = 0; i < buffer.length; i++) {            
            if(buffer[i].equals(obj) == true) {                
//                buffer[i] = buffer[i+1];                    
                Object[] tempBuf = new Object[maxIdx-1];
                System.arraycopy(buffer, 0, tempBuf, 0, i);                
                System.arraycopy(buffer, i+1, tempBuf, i, curIdx-i-1);
                buffer=tempBuf;
                curIdx--;
            }
        }
    }
}
 
class Controller <datatype> {
    datatype data;
    public void setData(datatype data) {
        this.data = data;
    }
    public datatype getData() {
        return data;
    }    
}
 
public class Start01 {
     
    public static void main(String[] args) {
        Controller<Logic> con = new Controller<Logic>();
        Scanner sc = new Scanner(System.in);
        Object msg = null;
        
        con.setData(new Logic(1));
        Logic logic = con.getData();
        while(true) {             
            System.out.println("[1]삽입 [2]조건검색 [3]전체조회 [4]삭제 [5]종료");
            System.out.print("원하는 작업 번호 : ");    
            int type = Integer.parseInt(sc.nextLine());            
            if(type == 5System.exit(0);            
            switch(type) {
            case 1:
                System.out.print("값을 입력하세요 : ");
                msg = sc.nextLine();    
                if(logic.add(msg)==trueSystem.out.println("삽입 성공~!");
                break;
            case 2:
                System.out.print("검색어를 입력하세요 : ");
                msg = sc.nextLine();
                logic.selectOne(msg);
                break;
            case 3:
                System.out.println("전체조회");
                logic.selectAll();
                break;
            case 4:
                System.out.println("삭제");
                msg = sc.nextLine();
                logic.delOne(msg);
                break;
            default:
                System.out.println("잘못 누르셨습니다.");
            }                        
        } 
    }
}
 
cs


반응형

'SKILL > JAVA' 카테고리의 다른 글

자바 String join  (0) 2017.12.22
자바 System.arraycopy() 원리  (0) 2017.12.21
자바 하노이의 탑 예제  (0) 2017.12.20
자바 메모리  (0) 2017.12.20
자바 Queue 기본 예제  (0) 2017.12.19