CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2007
    Posts
    4

    need help passing a vector to a separate class

    Here is my prob, ill try to put it as simple as possible.

    I have a class called DbObject where i have the vector ...

    public Vector<items> together = new Vector<items>(); //a vector of items objects

    items is a separate class that i have

    items contains a string called "name", as well as another vector called ans

    i want to put many instances of the items class in my "together" vector

    here is some code within my DbObject class

    items a = new items(names.elementAt(animalcounter),answers2);

    together.add(a);


    names is a vector and answers2 is a vector, both created in my DBObject class

    I want to be able to, within my DBObject class, do the following: get the "name" of any of the items instances within my together vector, and get any of the answers belonging to that given name

    System.out.println(together.get(0).name);
    System.out.println(together.get(0).answers.get(0));

    the first println works fine, i am able to get the name with ease, i've added many things to my together vector and get(index).name works just fine, but it seems like the answers2 vector is not being passed to the items class, the second println always results in an out of bounds exception.

    heres is some code for the items class

    public class items {


    public String name;
    public Vector<String> answers = new Vector<String>();


    public items(String Name, Vector ans){
    name = Name;
    answers = ans;


    }

    Is the items constructor correct for what I want to do?
    perhaps it's not as simple as doing answers=ans; ????


    is it even possible to have a vector that contains many vectors?


    any insight will be a big help, thank you.

  2. #2
    Join Date
    Oct 2007
    Posts
    4

    Re: need help passing a vector to a separate class

    additionally, my answers2 vector does have data in it, so thats not the problem.

    simply put, i want to be able to do System.out.println(together.get(index).answers.get(index)));
    where each different index for together.get(index) has an answers vector filled with my answers2 data.


    thanks

  3. #3
    Join Date
    Feb 2008
    Posts
    966

    Re: need help passing a vector to a separate class

    Quote Originally Posted by wishbone34 View Post
    I want to be able to, within my DBObject class, do the following: get the "name" of any of the items instances within my together vector, and get any of the answers belonging to that given name

    System.out.println(together.get(0).name);
    System.out.println(together.get(0).answers.get(0));

    the first println works fine, i am able to get the name with ease, i've added many things to my together vector and get(index).name works just fine, but it seems like the answers2 vector is not being passed to the items class, the second println always results in an out of bounds exception.
    Your second println is not how it works. If you have a Vector that contains Vectors of "item" objects you would access the First element, assuming not null, like this:

    together.get(0).get(0);

    The first together.get(0) gets the Vector<items> and the second one will get the first items object.

    HOWEVER, that being said, you should really do it like this:

    items item = null;

    Vector<items> temp = together.get(0);
    if(temp != null)
    item = temp.get(0);

    I haven't seen enough of your code to tell whether this will work or not because I don't know that you truely have a Vector of Vectors. Can you post up some more relevant code?

    Quote Originally Posted by wishbone34 View Post
    Is the items constructor correct for what I want to do?
    perhaps it's not as simple as doing answers=ans; ????
    Yes, your constructor is just fine. The only thing different you could do is say: "this.answer = ans;" but since you name them differently it really doesn't matter.

    Quote Originally Posted by wishbone34 View Post
    is it even possible to have a vector that contains many vectors?
    Of course it is. You can have a Vector of HashMaps where each element is a Vector that contains another Vector of ... till eternity if you so wish.

  4. #4
    Join Date
    Oct 2007
    Posts
    4

    Re: need help passing a vector to a separate class

    Still havent gotten to work...here is some more code, ive changed the answers2 vector to responses just for less confusion


    here is my items class once again


    public class items {


    public String name;
    public Vector answers = new Vector();


    public items(String Name, Vector ans){
    name = Name;
    answers = ans;


    }
    }




    //code within my DBObject class


    together.add(new items(names.get(animalcounter),responses));

    System.out.println(together.get(animalcounter).name);
    System.out.println(together.get(animalcounter).answers.get(3));

    responses.clear();

    animalcounter++;

    }//end while loop



    System.out.println(together.get(0).name);
    System.out.println(together.elementAt(1).answers.get(3));


    The top portion of the code is within a while loop that ends based on animalcounter.
    new items objects are added to the together vector(each items object is to contain a name, and a vector(this vector just contains strings)......the printlns inside the while loop work fine..... and the first println outside the loop works fine. The second one does not. It gives an out of bounds exception because there is nothing in the answers.get(3), the size is zero. I am doing a response.clear() because I need an empty vector for each iteration of the while loop because new info is put into the vector each time. And then that new info along with info from a name vector I have is being put into my together vector as an instance of my items class.

    Here is something odd.....If I do not do responses.clear(), naturally the vector ends up containing many more strings than I want........but it also allows the last println in my code to work! But unfortunately for all indexes of together.get(index) each answer vector is the exact same, and much larger than needed because I never cleared. When I dont clear it, my code works fine, the last println works, but each instance of together contains the same large array containing all of the possible data. When I do clear, my intermediate println works for each iteration of the while loop, but the last println does not because it says the array is empty. It's almost like clearing the responses vector is somehow clearing the ansers vector within the items class?



    What i want is........for my together vector, for each index of it, to contain a string variable called name and a vector of strings. each index of together will have a different name and its vector will also contain different strings.

  5. #5
    Join Date
    Oct 2007
    Posts
    4

    Re: need help passing a vector to a separate class

    Found a solution. Used a tmp vector created at the beginning of each while loop instead of using a public vector created at the beginning of the class.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured