-1

有人可以解释一下这两个程序之间的区别,基本上是泛型和对象之间的区别。还有为什么 System.out.println("add 1 to the set:" + vs.add(new String("Hello"))); 仅适用于第一个程序

第一个向量集程序

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());
}
}
4

1 回答 1

1

创建 VectorSet2 时,需要为其元素指定类型。在第二个程序的情况下,该类型是整数。字符串不是整数,因此不能将字符串添加到 VectorSet2。

但是,VectorSet1 可以包含任何类型的元素(任何可以被视为对象的元素)。因此,如果您有一个 VectorSet1,您可以向其中添加任何对象,无论是字符串、整数还是任何其他类型的对象。

于 2012-10-06T18:42:03.773 回答