Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning objects from collections 1

Status
Not open for further replies.

RiazKhanmohamed

Programmer
Oct 30, 2002
115
Hi, guess i'm still a newbie in this field really.

I've been recently introduced to collections from vb, so thought i'd give them a go in java. Perhaps I got the wrong end of the stick, but is this logic possible:

I have a class called Holiday, storing holiday information from a database.

I add a series of these to a collection (thus far i've tried HashSet, ArrayList and Vector).

I can list the counts, and check if specific holiday objects are in the collection.

But how do i get the holiday classes out of the collection? in vb it's automatically output as the original class. but in java it comes out as an object, cross-convertable to a class but not my class. Maybe i'm looking at the wrong end of the problem, but any guidance would be much appreciated. Thanks in advance
 
You have to cast it back into the type of object you want. I don't know why Java doesn't implement a template-like feature similar to how they are in C++, but it doesn't.
 
If you know what classes can be returned by the collection (because you know what you have filled in, filling in anything else should not be possible if you have good encapsulation anyway), you can also check what specific class it is with a instanceof operator.

The template-like feature comes with Java 1.5 and is called generics, details by overview or in-depth.

If you need more in-depth coverage, a look at the reflection API may be worth it. An overview, and the API tutorial. Also, the Class class (class called "Class") is pretty powerful.

Hope this helps, feel free to post if you have specific questions, I've already worked with reflection and many other members most probably have as well...

haslo@haslo.ch - www.haslo.ch​
 
Awesome, it's about time they released generics. Now if they would only allow operator overloading...
 
My guess is that this will not happen, unfortunately. Makes me sad as well. But it is a design decision, I heard they decided it was sometimes confusing (when programmers used operators for completely unintended purposes) and made the language harder to use (because you don't know what exactly an operator does when you see it). But you can just as well create a function called "add" that subtracts, why should that not be possible for operators?

Alas, it's not in our hands [noevil]

haslo@haslo.ch - www.haslo.ch​
 
FredPerry,

just cast it as the class you want:
Code:
Vector v = new Vector();
v.add(new Holiday());

Holiday h = (Holiday) v.get(0);

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
 
Heh, sorry, I was up in the clouds, I guess :)

haslo@haslo.ch - www.haslo.ch​
 
Thanks guys. Been away from a pc for a few days. All that sound great, though at the moment i think i'll stick with java v1.4.2.

jemminger: i'll try that code, seems like what I want, though my next question would have been to ask how to cast objects. I'm still getting my head around it all, you've been a great help. I'll give some stars when/if I get it working.
 
ImpureNokio :

Is the code you posted just not cut'n'past'ed from jemminger's example (which you should use if not using 1.5) ?!!

--------------------------------------------------
Free Database Connection Pooling Software
 
Thanks jemminger. I think i'll stick to 1.4.2 and use that casting method.


ImpureNokio: I said it would have been my next question, but jemminger answered it. and yes, you did copy it exactly from jemminger, i could already see his answer. However the links may prove useful.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top