Home arrow static arrow Java Programming [Archive] - Getting values from a collection
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Getting values from a collection
This topic has 2 replies on 1 page.

Posts:65
Registered: 7/13/04
Getting values from a collection  
Jul 21, 2004 3:48 PM



 
I am iterating through a collection successfully , but I am unclear as to how I can access the values of the object in my collection. I have a UserBO class with various getters and setters. I set a userName in that object and then that object is assigned to a collection named auth. So auth contains the collection of UserBO objects. Here is what I have to iterate over the collection now (it works).

//iterate over the collectin to get the values			Collection list = auth;			String user;			Iterator iter = list.iterator();			while (iter.hasNext()) {				Object obj = iter.next();				if (obj != null) {					user  = (String) obj.toString();					System.out.println("the value is: " + user);				}			}


Now, obj holds my UserBO object (I can see this when the println is called). How do i get the values out of the obj? How would get my userName for instance?
 

Posts:31,095
Registered: 4/30/99
Re: Getting values from a collection  
Jul 21, 2004 3:58 PM (reply 1 of 2)



 
Cast it.
UserBO userThing = (UserBO)iter.next();
And by the way, while we are on the topic of casting, the cast is unnecessary in this line of code:
user  = (String) obj.toString();
The toString() method returns a String already. This is sufficient:
user = obj.toString();
PC²
 

Posts:65
Registered: 7/13/04
Re: Getting values from a collection  
Jul 21, 2004 4:06 PM (reply 2 of 2)



 
Awsome, that worked. I am new to the idea of casting, (PHPer) hence my missuse of it. I'm learning slowly but surely. Thanks a lot. : )
 
This topic has 2 replies on 1 page.