Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8664553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:16:06+00:00 2026-06-12T17:16:06+00:00

can someone explain me the difference between the 2 programs, basically the difference between

  • 0

can someone explain me the difference between the 2 programs, basically the difference between generic and object.
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());
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:16:07+00:00Added an answer on June 12, 2026 at 5:16 pm

    When you create a VectorSet2, you specify a type for its elements. In the case of the second program, that type is Integer. A String is not an Integer, so you can’t add a String to a VectorSet2.

    A VectorSet1, though, can have elements of any type (anything that can be treated as an Object). So if you have a VectorSet1, you can add any object to it, whether it’s a String, Integer, or any other kind of object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone explain the difference between these two, the first one is taken from
Can someone explain the difference between apache mod_wsgi in daemon mode and django fastcgi
Can someone explain the difference between static NSString* CellIdentifier = @Cell; and NSString *CellIdentifier
Can someone please explain to me difference between extends and super in java generics
Can someone explain this to me? In particular the difference between: http://github.com/whymirror/greg and http://piumarta.com/software/peg/
Can someone please explain to me the difference between the AppSettings and ApplicationSettings sections
Can someone please explain with example that I can understand about the difference between
CWM is data modeling UML is object modeling. Can someone explain the difference that
Can someone explain the difference between IN and EXISTS and NOT IN and NOT
Can someone explain the difference between .last() and :last ? I can't seem to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.