Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 19th, 2003, 05:38 PM
zapa zapa is offline
Mentat of IX
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: Bucuresti / Toronto
Posts: 112 zapa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 sec
Reputation Power: 6
Send a message via ICQ to zapa
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

Reply With Quote
  #2  
Old May 20th, 2003, 03:05 AM
walter76 walter76 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 18 walter76 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
  #3  
Old May 20th, 2003, 08:06 AM
zapa zapa is offline
Mentat of IX
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: Bucuresti / Toronto
Posts: 112 zapa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 sec
Reputation Power: 6
Send a message via ICQ to zapa
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Accessing Vector Elements


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway