Results 1 to 5 of 5

Thread: Vector

  1. #1
    Join Date
    Sep 2003
    Location
    Brussel
    Posts
    52

    Unanswered: Vector

    I have a vector of objects. Now I want to get the object out of the vector. So I do:

    for (int i = 0; i < vectorname.size(); i++) {
    vectorname.elementAt(i);
    }

    If I do that I should get the object at the given index. But how can I get the object's variables?

    For example a vector of Person objects and a Person has got a name and an age. Now how do I get the name and the age?

    Like this?

    for (int i = 0; i < vectorname.size(); i++) {
    Person pers = new Person();
    pers = vectorname.elementAt(i);
    System.out.println(pers.getName() + pers.getAge());
    }

    What should I do? Any help is welcome. Also if you know some good tutorials, just let me know. I can use it all.

    Thanks in Advance!!!
    A good programmer is a LAZY programmer!

  2. #2
    Join Date
    Jan 2004
    Location
    Sochaux (France)
    Posts
    55
    personnally, i should do like this :
    PHP Code:

    for (int i 0vectorname.size(); i++) {
    Person pers vectorname.elementAt(i);
    System.out.println(pers.getName() + pers.getAge());

    Or maybe using vectorname.elementAt(i).getName(); but i'm not sure it works...

    For all documentation about vector, u can use http://java.sun.com/j2se/1.4.2/docs/api/index.html

  3. #3
    Join Date
    Sep 2003
    Location
    Brussel
    Posts
    52
    I do it that way, but it gives me an error. Incompatible types, found: java.lang.Object, required: tab.TabColumn;

    tab.TabColumn is the class of the objects that should be in the Vector. So I don't see the problem. this is my code:

    public void PrintVector (Vector tabProp) {
    for (int i = 0; i < tabProp.size(); i++) {
    TabColumn tbc = tabProp.elementAt(i);
    System.out.println(tbc.getTabName);
    }
    }

    TabColumn has got a private datamember tabname and a number of methods, including a getTabName, that should return the private data member.

    Any kind of help is welcome ...
    A good programmer is a LAZY programmer!

  4. #4
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1
    Originally posted by Lazy
    I do it that way, but it gives me an error. Incompatible types, found: java.lang.Object, required: tab.TabColumn;

    ...

    public void PrintVector (Vector tabProp) {
    for (int i = 0; i < tabProp.size(); i++) {
    TabColumn tbc = tabProp.elementAt(i);
    System.out.println(tbc.getTabName);
    }
    }

    If you look at Vector's javadoc you'll see that elementAt() retruns an Object. You have to explicitly cast it to TabColumn:

    Code:
     TabColumn tbc = (TabColumn) tabProp.elementAt(i);

  5. #5
    Join Date
    Sep 2003
    Location
    Brussel
    Posts
    52
    Thanks
    A good programmer is a LAZY programmer!

Posting Permissions

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