Results 1 to 3 of 3

Thread: vector add

  1. #1
    karish is offline Member
    Join Date
    Dec 2012
    Rep Power

    Default vector add

    I am trying to add an object to the vector, the object consists of 2 elements, str1 and str2. i want the vector to add the object only if str1 doesn't repeat itself, in other words no duplicates of str1. str2 can have the same element.

    please help

    Java Code:
    public class main {
    	public static void main(String[] args) {
    		A a = new A();
    		B b = new B("AA","GGG");
    		b = new B("BB","LLL");
    		b = new B("AA","PPP");
    		b = new B("DD","LLL");
    Java Code:
    public class B {
    	private String str1;
    	private String str2;
    	public B(String str1, String str2){
    		this.str1 = str1;
    		this.str2 = str2;
    	public String getStr1(){
    		return this.str1;
    	public String getStr2(){
    		return this.str2;
    	public String toString(){
    		return str1+" "+str2;
    Java Code:
    public class A {
    	Vector vec = new Vector();
    	public void addToVector(Object obj){
    		for(Iterator iter = vec.iterator(); iter.hasNext();){
    			Object o =;
    	public String toString(){
    		return vec.toString();
    Last edited by karish; 12-13-2012 at 10:39 PM.

  2. #2
    Potato is offline Member
    Join Date
    Dec 2011
    Rep Power

    Default Re: vector add

    Are you using Vector class for any particular reason? And in this situation in the method addToVector, you want to cast your 'Object' to 'B' (you should be using Generics btw) and check if the first String in B matches the first String in 'obj' (Which should also be 'B' and not 'Object')

    Also look at:
    Last edited by Potato; 12-13-2012 at 08:57 PM.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    Default Re: vector add

    Moved from Advanced Java

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Vector
    By Anandiscool in forum New To Java
    Replies: 4
    Last Post: 03-28-2010, 08:10 PM
  2. Vector<vector> loop thru
    By ocean in forum New To Java
    Replies: 11
    Last Post: 11-21-2009, 02:17 PM
  3. Vector
    By sanox in forum New To Java
    Replies: 20
    Last Post: 09-01-2009, 04:21 PM
  4. Replies: 1
    Last Post: 04-06-2009, 06:45 AM
  5. Vector help
    By king_arthur in forum New To Java
    Replies: 3
    Last Post: 01-22-2008, 07:33 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts