오보에블로그

Collection & Generics 본문

STEADYSTUDY/Etc

Collection & Generics

(OBO) 2017. 11. 16. 09:23
728x90

collection 이란 java 에서 데이터를 자료구조하기위해 기본적으로 제공하는것

import java.util.*; 

을 통해 불러서 사용할 수 있다.

Vector<Integer> yoong = new Vector<Integer>(); 

을 통해 yoong이라는 정수를 요소로 하는 vector 구조를 생성 할 수 있다. <> 안에는 데이터 타입의 wrapper, 클래스 형태로 써야한다.

ex) int -> Integer

  String ->String




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
import java.util.*;
 
class Dic{
    HashMap<Integer, String> map = new HashMap<Integer, String>(); //생성
    Dic(){ //생성자 초기화
        map.put(1"one");
        map.put(2"two");
        map.put(3"three");
        map.put(4"four");
        map.put(5"five");
        map.put(6"six");
        map.put(7"seven");
        map.put(8"eight");
        map.put(9"nine");
        map.put(10"ten");
        
    }
    public String numToEng(int num) {//키 값의 value 리턴
        return map.get(num);
            
            
    }
    
    
}
 
public class main {
    public static void main(String[] args) {
        Dic dic = new Dic();
        System.out.println( dic.numToEng(3));
        System.out.println(dic.numToEng(9));
    }
}
 
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
67
68
import java.util.*;
 
class Box<T>{
    private ArrayList<T> obo = new ArrayList<T>();
    
    public Box() {//생성자
        
    }
    public void add(T t) {
        
        obo.add(t);//Type 이 T인 t를 obo에 추가
        
    }
    public T get(int index) {
        return obo.get(index);//obo의 index번째 값 리턴
    }
    public int get_size() {
        return obo.size();
        
    }
    
    public void print() {
        Iterator<T> it = obo.iterator();//obo의 처음 원소 앞에를 가르킴
        while(it.hasNext()) {// it의 다음에 원소가 존재하면 true, 아니면 false,
            System.out.println(it.next()); //it 이 다음 원소로 옮겨지고 it 값리턴
        }
        
    }
}
 
public class BoxPlayer {//잘되는지 테스트
    public static void main(String args[]) {
        Box<Integer> bi = new Box<Integer>();
        bi.add(10);
        bi.add(50);
        bi.add(100);
        
        System.out.println(bi.get(2));
        System.out.println(bi.get_size());
        bi.print();
        
        System.out.println("---------------------");
        
        Box<String> bs = new Box<String>();
        bs.add("one");
        bs.add("two");
        bs.add("three");
        
        System.out.println(bs.get(2));
        System.out.println(bs.get_size());
        bs.print();
        
        System.out.println("--------------------");
        
        Box bb = new Box();
        bb.add("one");
        bb.add("two");
        bb.add("3");
        
        System.out.println(bb.get(2));
        System.out.println(bb.get_size());
        bb.print();
    }
 
}
 
 
 
cs



틀린 말이 있을 시 지적해주시면 감사하겠습니다.

728x90

'STEADYSTUDY > Etc' 카테고리의 다른 글

2447_별찍기 - 10  (0) 2018.02.06
자바실습 Week10 - FILE IO  (0) 2017.11.22
추상클래스 (abstract class) 사용  (0) 2017.11.08
클래스 상속과 객체  (0) 2017.11.01
week6:class  (0) 2017.10.25