0
my code:

class A{
Vector<String> s = new Vector<String>();
}

public class Vec {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

A a = new A();

    a.s.add("String1");
    a.s.add("String2");
    a.s.add("String3");
    a.s.add("String4");
    a.s.add("String5");
    Vector<A> vecA = new Vector<A>();
    vecA.add(a);

    System.out.println("Size of vector is "+vecA.size()); 

    a = new A();
    a.s.add("String6");
    a.s.add("String7");
    a.s.add("String8");
    a.s.add("String9");
    a.s.add("String10");
    vecA.add(a);

 System.out.println("Size of vector is "+vecA.size()); 

 for(int i=0;i<vecA.size();i++)
 {
  System.out.println(vecA.elementAt(i).s.elementAt(0));
  System.out.println(vecA.elementAt(i).s.elementAt(1));
  System.out.println(vecA.elementAt(i).s.elementAt(2));
  System.out.println(vecA.elementAt(i).s.elementAt(3));
  System.out.println(vecA.elementAt(i).s.elementAt(4));
 }


 boolean b =  vecA.contains("String1");
 System.out.println(b);
 if(vecA.contains("String1")){
     System.out.println("true");
 }else{
     System.out.println("false");
 }

}
}
my problem is:
how to search for particular string in vecA ?
i am doing in this way 
boolean b = vecA.contains("String1");
when ever i excute i am getting false in variable 'b'. But "String1" is already added in vector vecA.

please show me my fault and also guide me how to search for a vector of class types.

Thanks,pavan.
3
Contributors
2
Replies
17
Views
3 Years
Discussion Span
Last Post by milil
0

vecA is a Vector of A objects, so there's no way it can contain a String.

contains(...) just searches the Vector's elements for an equals() match, it does not search inside any "inner" Vectors that are in the "outer" Vector.
For that you would need to do some kind of recursive search of all the Collections in the outer Vector

0

You can do like this:

for (int i = 0; i< vecA.size(); i++) {
            A newA = vecA.elementAt(i); // you get clas A 
            Vector newVector = newA.s; // you grab vector from clas A

            if (newVector.contains("String1")) {  // is that vector contains that string? ...
                System.out.println("We found it...!");
            }

}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.