Results 1 to 3 of 3
Thread: vector add
- 12-12-2012, 11:22 PM #1
Member
- Join Date
- Dec 2012
- Posts
- 1
- Rep Power
- 0
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 10:39 PM.
- 12-13-2012, 08:55 PM #2
Member
- Join Date
- Dec 2011
- Posts
- 25
- Rep Power
- 0
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.htmlLast edited by Potato; 12-13-2012 at 08:57 PM.
- 12-14-2012, 03:00 AM #3
Similar Threads
-
Vector
By Anandiscool in forum New To JavaReplies: 4Last Post: 03-28-2010, 08:10 PM -
Vector<vector> loop thru
By ocean in forum New To JavaReplies: 11Last Post: 11-21-2009, 02:17 PM -
Vector
By sanox in forum New To JavaReplies: 20Last Post: 09-01-2009, 04:21 PM -
Vector<Point2D> list = new Vector<Point2D>();Is it possible for 15,000 Point2D obj???
By Mazharul in forum Java 2DReplies: 1Last Post: 04-06-2009, 06:45 AM -
Vector help
By king_arthur in forum New To JavaReplies: 3Last Post: 01-22-2008, 07:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks