USEme, plz 2020. 4. 6. 16:58
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
// arraylist add로 추가하고 get으로 뺴냄
 
package Day_15;
 
import java.util.ArrayList;
 
public class Day15Ex02 {
 
    public static void main(String[] args) {
        // ArrayList는 배열과 유사하다.
        // 값의 중복을 허용한다.
        // 요소 접근은 index로 한다.
        
        
        ArrayList<String> list  = new ArrayList<>();
        list.add("오징어");
        list.add("꼴뚜기");
        list.add("대구");
        list.add("명태");
        list.add("거북이");
 
        System.out.println(list.size());
        System.out.println(list);
        
        // add()는 append() 기능과 insert() 기능이 모두 있다.
        list.add(2,"상어");
        System.out.println(list);
        //[오징어, 꼴뚜기, 상어, 대구, 명태, 거북이]
        
        System.out.println(list.get(4));  // 명태. 그 요소에 들어있는 값이 무엇인지 찾아낸다.
        System.out.println(list.indexOf("명태"));  // 4
        
        // 수정 - set()
        list.set(4"고래");
        System.out.println(list);
        //[오징어, 꼴뚜기, 상어, 대구, 고래, 거북이]
        
        
        // 삭제 - remove
        list.remove(1);
        System.out.println(list);
        
        
    }
}
cs
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
 
// map
// 맵은 key와 벨류가 쌍으로 묶어있다
 
package Day_15;
 
import java.util.Hashtable;
 
public class Day15Ex03 {
 
    public static void main(String[] args) {
        // Map인터페이스를 상속받아서 >>> HastTable 컬렉션 클래스를 만든다. 
        // 맵구조는 Key ; Value가 쌍으로 요소가 구성 된다.
        Hashtable<StringString> map = new Hashtable<>();
        
        
        // put으로 집어넣고 get으로 참조
        map.put("홍길동""0");
        map.put("이길동""1");
        map.put("김길동""2");
        map.put("박길동""3");
        
        // System.out.println(map);
        System.out.println(map.get("김길동")); // 하나씩 접근할때. 해당 키값이 가르키는 벨류를 가지고온다.
        
        
        
    }
 
}
cs
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
 
// map
// 맵은 key와 벨류가 쌍으로 묶어있다
 
package Day_15;
 
import java.util.Hashtable;
import java.util.Scanner;
 
import Day15_test.MyPoint;
 
interface View {
    void display(); 
}
 
// 인터페이스를 가지고 입력 출력 종료를 구현한다,
class Input implements View {
    public void display() {
        System.out.println("입력기능");
    }
        
}
 
class Output implements View {
    public void display() {
        System.out.println("입력기능");
    }
            
}
        
        
class End implements View {
    public void display() {
        System.out.println("프로그램 종료");
        System.exit(0);
                
    }
}
 
public class Day15Ex3_1 {
    static Hashtable<Integer, View> mapping = new Hashtable<>(); // View > input,output . . .
    static {
        mapping.put(1new Input());
        mapping.put(2new Output());
        mapping.put(3new End());
    }
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        while (true) {
            System.out.println("입력(1), 출력(2), 종료(3) >>>");
            int no = scanner.nextInt();
            if (no < 1 || no > mapping.size()) {
                System.out.println("잘못된 입력입니다.");
            } else {
                //if(no==3) break;
                mapping.get(no).display(); // display에서 직접 했기때문에 println이 필요없다
            } 
            //System.out.print(mapping.get(no));
        }
        
    }
 
}
 
cs