Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

vector add

$
0
0
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

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);
		
	}

}


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;
	}
}



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();
	}
}

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>