본문 바로가기

카테고리 없음

vector

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
package vector;
 
import java.util.Iterator;
import java.util.Vector;
 
public class Ex01 {
 
    public static void main(String[] args) {
        // 정수값만 다루는 제네릭 백터 생성
        // 타입 매개변수를 Integer 타입으로 구체화시켰다. <E>  -> <Integer>
        Vector<Integer> v = new Vector<Integer>(7);
        
        
        // 데이터 추가 (append) - 전부 오토박싱
        v.add(5);
        v.add(4);
        v.add(-1);
        
        // 삽입. (중간에 삽입한다. insert) 
        // add에는 두 가지 기능이 있다. < 추가 / 삽입 >
        v.add(2,100);
        System.out.println(v);
        
        
        System.out.println(v.size());
        
        // 백터 선언시 capacity를 지정하지 않으면 기본적으로 capacity 10을 지급한다. 
        System.out.println(v.capacity());
        
        
        // 백터를 출력한다. 
        System.out.println(v.get(2));
        
        for(int i = 0; i < v.size(); i++) {
            int n = v.get(i); // 자동언박싱
            System.out.print(n + " ");
            
        }
        System.out.println();
        for(Integer i : v ) {
            System.out.print(i + " ");
        }
        
        System.out.println();
        Iterator<Integer> it = v.iterator();
        while(it.hasNext()) {
            System.out.println(it.next() + " ");
        }
    }
}
 
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
package vector;
 
import java.util.Vector;
 
class Point {
    
    private int x, y;
    // 생성자 오버로딩 
    public Point (int x, int y) {
    
    this.x = x;
    this.y = y;
    
    }
    // toString 메소드 오버라이드
    @Override
    public String toString() {
        return "(" +x+ "," +y+ ")";
    }
    
}
 
public class Ex02 {
    public static void main(String[] args) {
        Vector<Point> v = new Vector<Point>();
        
        // 3개의 point 객체삽입
        v.add(new Point(2,3));
        v.add(new Point(-5,20));
        v.add(new Point(30,-8));
        
        v.remove(1); // 인덱스 1의 Point(-5,20) 삭제
        
//        
//        for(Point p : v) {
//            System.out.println(p);
//    }
        
        
        // 함수로 출력하기
        printVextor(v);
        
    }
    public static void printVextor(Vector<Point> v) {
        for(Point p : v) {
            System.out.println(p);
        }
        
    }
}
cs