|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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 ![]() |
|
#3
|
|||
|
|||
|
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 |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Java Help > Accessing Vector Elements |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|