22일차 강의 정리


소스1 * import 는 import java.util.*; 로 고정

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
public class Ex01 {
    public static void main(String[] args) {
        //Vector
        //data 관리 -  정보 관리
        //변수,,배열(정적),,->동적할당
        //jdk 1.0 ~ Vector(List, Set) - 입력, 삭제
        //List - 순서, 인덱스, 값의 중복, 수정, 추가
        //Set - 집합, 순서x, 중복x, 수정,추가개념 x
        
        Vector vec = new Vector();
        System.out.println("1 capasity: "+vec.capacity());    //처음 공간 10
        vec.add("일");
        vec.add("이");
        vec.add("삼");
        vec.addElement("사");
        
        vec.trimToSize();    //없는 공간 자르기
        System.out.println("2 capasity: "+vec.capacity());    //잘라내서 저장공간4
        
        vec.add("이");
        System.out.println("3 capasity: "+vec.capacity());    //추가하면 +4(기존용량의 2배로증가)
        vec.add(3);
        vec.addElement(4);
        System.out.println("4 capasity: "+vec.capacity());    
        System.out.println("---------------------------------");
        System.out.println("size: "+vec.size());    //실제들어있는 용량
        
        vec.remove(1);                //idx번호 1 삭제
        vec.removeElement("삼");    //() 값 삭제
        //출력방법 1
        for (int i = 0; i < vec.size(); i++) {
            System.out.print(vec.get(i)+" ");
        }
        System.out.println("\n---------------------------------");
        //출력방법 2
        Enumeration enu = vec.elements();
        while(enu.hasMoreElements()){
            System.out.println(enu.nextElement());
        }
 
    }
}
cs

처음 용량을 10으로 할당된다.

처음의 용량을 넘어서 값이 추가 된다면 현재의 용량의 X2 로 증가한다.

결과1

소스2

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
public class Ex02 {
    public static void main(String[] args) {
        Vector vec = new Vector(53);        //(초기용량, 증가량)
        System.out.println("1 capasity: "+vec.capacity());
        System.out.println("size: "+vec.size());    //실제들어있는 용량
        
        vec.add(1);
        vec.add(2);
        vec.add("일");
        vec.add("이");
        vec.add("삼");
        System.out.println("2 capasity: "+vec.capacity());
        System.out.println("size: "+vec.size());
        
        vec.add("사");    //초기용량 5를 넘어선순간 증가량+3을 더해 공간이 8로 증가
        System.out.println("3 capasity: "+vec.capacity());
        System.out.println("size: "+vec.size());
        
        System.out.println("--------------------------");
        System.out.println("인덱스번호(2)의 값: "+vec.get(2));
        System.out.println("인덱스번호(3)의 값: "+vec.elementAt(3));
        System.out.println("\"3\"이 있는가? "+vec.contains(new Integer(3)));//내부에 값이 있는지 true/false 
        System.out.println("내용(이)의 인덱스번호 : "+vec.indexOf("이"));    //"이"라는 값의 인덱스번호 출력
        
        System.out.println("--------------------------");
        Enumeration enu = vec.elements();
        while(enu.hasMoreElements()){
            System.out.print(enu.nextElement()+" ");
        }
        System.out.println();
        //커서가 끝까지 가 있어서 다시출력하기위해서는 객체다시생성필요
//        System.out.println("---------------------------------------------------");
//        enu = vec.elements();        
//        while(enu.hasMoreElements()){
//            System.out.println(enu.nextElement());
//        }    
        System.out.println("--------------------------");
//        vec.clear();        //모든 내용 삭제
//        vec.trimToSize();    //모든 용량 삭제
        System.out.println("3 capasity: "+vec.capacity());
        System.out.println("size: "+vec.size());
 
        System.out.println("--------------------------");
        System.out.println("--------------------------");
        Iterator ite = vec.iterator();        //Enumeration와 동일 커서
        while(ite.hasNext()){
            System.out.print(ite.next()+" ");
//            ite.remove();        //vec 내용을 삭제
//        }
//        System.out.println("--------------------------");
//        System.out.println("--------------------------");
//        ite = vec.iterator();        //Enumeration와 동일 커서
//        while(ite.hasNext()){
//            System.out.println(ite.next());    //remove 로 삭제되어 아무것도 출력없음
//            ite.remove();
        }
        System.out.println();
        System.out.println("=======================================");
        Object[] arr = new Object[vec.size()+1];
        vec.copyInto(arr);        //백터 -> 배열로
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");        //null값은 null로 출력
        }
 
    }//main end
}//class end
cs

결과2


+ Recent posts