2 Replies - 335 Views - Last Post: 05 October 2012 - 03:00 PM Rate Topic: -----

#1 koricha  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 08-February 12

Java Vector set

Posted 05 October 2012 - 12:51 PM

can someone explain me the difference between the 2 programs.
also why
System.out.println("adding 1 to the set: " + vs.add(new String("Hello")));

works only for the first program

First Vector set program
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());
	}
}


Second Vector set program
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());
	}
}

This post has been edited by koricha: 05 October 2012 - 01:41 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Java Vector set

#2 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Java Vector set

Posted 05 October 2012 - 01:55 PM

So you are asking the difference between generic and object?
There are minor differences. If you learn more about both you can see the advantages and disadvantages of both.

As for why your add new String will not work.

//This allows your vector to only accept Integer types.
VectorSet2<Integer> vs = new VectorSet2<Integer>();


Was This Post Helpful? 0
  • +
  • -

#3 koricha  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 08-February 12

Re: Java Vector set

Posted 05 October 2012 - 03:00 PM

yes i am asking the difference between generic and object, also how can i add "Hello" to the second program without changing the object name
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1