2012-10-06 5 views
-1

누군가 제게 두 프로그램의 차이점을 설명 할 수 있습니까? 기본적으로 일반과 객체의 차이점이 있습니다. 도 이유가 System.out.println ("1을 세트에 추가 :"+ vs.add (새 문자열 ("Hello")))); 첫 번째 프로그램Java Vector Set

최초의 벡터 설정 프로그램

import java.util.Vector; 

class VectorSet1 { 
boolean add(Object obj) { 
     if (contains(obj)) return false; 
    v.add(obj); 
    return true; 
} 

boolean contains(Object obj) {return v.contains(obj);} 

public String toString() {return v.toString();} 

void clear() {v.clear();} 

int size() {return v.size();} 

boolean isEmpty() {return v.isEmpty();} 

Vector v = new Vector(); 

public static void main(String [] args) { 
    VectorSet1 vs = new VectorSet1(); 

    System.out.println("set: " + vs); 

    System.out.println("adding 1 to the set: " + vs.add(1)); 
    System.out.println("adding 5 to the set: " + vs.add(5)); 
    System.out.println("adding 17 to the set: " + vs.add(17)); 
    System.out.println("adding 1 to the set: " + vs.add(1)); 
    //System.out.println("adding 1 to the set: " + vs.add(new String("Hello"))); 

    System.out.println("set: " + vs); 

    System.out.println("testing if 1 s in the set: " + vs.contains(1)); 
    System.out.println("testing if 17 is in the set: " + vs.contains(17)); 
    System.out.println("testing if 6 is in the set: " + vs.contains(6)); 

    System.out.println("set is empty: " + vs.isEmpty()); 
    System.out.println("size of set: " + vs.size()); 


    vs.clear(); 

    System.out.println("after invoking clear"); 

    System.out.println("set: " + vs); 

    System.out.println("set is empty: " + vs.isEmpty()); 
    System.out.println("size of set: " + vs.size()); 
} 
} 

두 번째 벡터 설정 프로그램

import java.util.Vector; 
import java.util.Iterator; 

class VectorSet2<E> { 
boolean add(E e) { 
     if (contains(e)) return false; 
    v.add(e); 
    return true; 
} 

boolean contains(E e) {return v.contains(e);} 

public String toString() {return v.toString();} 

void clear() {v.clear();} 

int size() {return v.size();} 

boolean isEmpty() {return v.isEmpty();} 

Vector<E> v = new Vector<E>(); 

public static void main(String [] args) { 
    VectorSet2<Integer> vs = new VectorSet2<Integer>(); 

    System.out.println("set: " + vs); 

    System.out.println("adding 1 to the set: " + vs.add(1)); 
    System.out.println("adding 5 to the set: " + vs.add(5)); 
    System.out.println("adding 17 to the set: " + vs.add(17)); 
    System.out.println("adding 1 to the set: " + vs.add(1)); 
    //System.out.println("adding Hello to the set: " + vs.add("Hello")); 

    System.out.println("set: " + vs); 

    System.out.println("testing if 1 s in the set: " + vs.contains(1)); 
    System.out.println("testing if 17 is in the set: " + vs.contains(17)); 
    System.out.println("testing if 6 is in the set: " + vs.contains(6)); 

    System.out.println("set is empty: " + vs.isEmpty()); 
    System.out.println("size of set: " + vs.size()); 


    vs.clear(); 

    System.out.println("after invoking clear"); 

    System.out.println("set: " + vs); 

    System.out.println("set is empty: " + vs.isEmpty()); 
    System.out.println("size of set: " + vs.size()); 
} 
} 
+5

http://docs.oracle.com/javase/tutorial/java/generics/ – gtgaxiola

+1

코드에 대한 좋은 설명을 묻지 않고 두 예제 모두 이해하기 어려운 부분을 물어보십시오. –

답변

1

에 대한 작품은 당신이 VectorSet2을 만들 때 그 요소의 유형을 지정합니다. 두 번째 프로그램의 경우 해당 유형은 정수입니다. 문자열은 정수가 아니므로 String을 VectorSet2에 추가 할 수 없습니다.

그러나 VectorSet1에는 모든 유형의 요소 (Object로 처리 할 수있는 모든 요소)가있을 수 있습니다. 따라서 VectorSet1을 사용하는 경우 String, Integer 또는 다른 종류의 객체이든 관계없이 객체를 추가 할 수 있습니다.