Results 1 to 3 of 3

Thread: vector add

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

    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");
    		a.addToVector(b);
    		
    		b = new B("BB","LLL");
    		a.addToVector(b);
    		
    		b = new B("AA","PPP");
    		a.addToVector(b);
    		
    		b = new B("DD","LLL");
    		a.addToVector(b);
    
    		System.out.println(a);
    		
    	}
    
    }
    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 = iter.next();
    			if(o.equals(obj)){
    				break;
    			}
    		}
    		vec.add(obj);
    	}
    	
    	public String toString(){
    		return vec.toString();
    	}
    }
    Last edited by karish; 12-13-2012 at 11:39 PM.

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

    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: http://docs.oracle.com/javase/6/docs...l/HashMap.html
    Last edited by Potato; 12-13-2012 at 09:57 PM.

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

    Default Re: vector add

    Moved from Advanced Java

    db
    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, 09:10 PM
  2. Vector<vector> loop thru
    By ocean in forum New To Java
    Replies: 11
    Last Post: 11-21-2009, 03:17 PM
  3. Vector
    By sanox in forum New To Java
    Replies: 20
    Last Post: 09-01-2009, 05:21 PM
  4. Replies: 1
    Last Post: 04-06-2009, 07:45 AM
  5. Vector help
    By king_arthur in forum New To Java
    Replies: 3
    Last Post: 01-22-2008, 08: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
  •