#1
  1. No Profile Picture
    Mentat of IX
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Location
    Bucuresti / Toronto
    Posts
    112
    Rep Power
    12

    Accessing Vector Elements


    Hi There ,

    I'm trying to build a vector full of objects that are made from subclasses written by me . Everything works fine , except I don't know , nor have I been able to find a clear example of how to extract objects out of a Vector and then use them .

    At this point i create a vector

    Vector elements = new Vector();
    Element e = new Element();
    elements.addElement(e);

    I have a method which is supposed to return a String which is part of the e object . Let's say :
    return e.stringVariable;

    how can I extract a certain object out of the vector and access one of it's fields . I tried the obvious :

    Element e = elements.elementAt(1);
    return e.stringVariable;

    but thjat doesn't seem to work . How should this be done ?


    Thanks
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    18
    Rep Power
    0
    Getting one element of class Element at index from the vector elements:
    Code:
    Element e = (Element) elements.get(index);
    Getting the size (number of elements) of the vector:
    Code:
    elements.size();
    Going through all the elements in the vector:
    Code:
    Iterator it = elements.iterator();
    while(it.hasNext()) {
      Element e = (Element) it.next();
      // do something with the element
    }
    Sometimes it's quite helpfull to look at this Java SDK API Documentation
  4. #3
  5. No Profile Picture
    Mentat of IX
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Location
    Bucuresti / Toronto
    Posts
    112
    Rep Power
    12
    thanks ,
    belive me , I try to cover all the angles and check all I could check before asking a question .
    I did look at the documentation , but no clear examples were given about extracting an object out of it and using it's methods .

    I had a good ideea of it's methods , i knew which 2 methods i shold look at .get() and .elementAt() however there was no practical example anywhere

IMN logo majestic logo threadwatch logo seochat tools logo