new Java problem: storing class instances

From Visual Basic to GNU C, this is the place to talk programming.

Moderators: SecretSquirrel, just brew it!

new Java problem: storing class instances

Postposted on Sat Jul 31, 2004 7:30 pm

If you didnt' like my last question, maybe you'll like this one...

I need to store class instances in a Vector. How do I do that? I tried, but Vector changes everything to an Object. How do I keep my own data types?

or else I'm just not understanding what's going on...

I have to work with things of type FileMonitor. So I have some method return types as FileMonitor. The Java compiler complains, and says that the returned type, an Object (from Vector) is not a FileMonitor.

Any ideas? Come on, there must be at least a few Java junkies here on TR...
FireGryphon
Darth Gerbil
Gold subscriber
 
 
Posts: 7342
Joined: Sat Apr 24, 2004 7:53 pm
Location: the abyss into which you gaze

Postposted on Sat Jul 31, 2004 7:37 pm

Typecast the Object to the correct type using (class name) when getting them from the vector.

Code: Select all
return (FileMonitor) v.elementAt( index );
Yahoolian
Grand Gerbil Poohbah
 
Posts: 3576
Joined: Sun Feb 16, 2003 3:43 pm
Location: MD

Postposted on Sat Jul 31, 2004 8:33 pm

And you're posting these questions in the Back Porch, and not Developer's Den, because...?

Vector doesn't "change" anything -- it uses object as a way to have a loosely typed collection. You may want to look into "generics" to find a way to create a more strongly typed collection. Though if you're throwing several different kinds of isntances into it (which would be a bad idea anyway) that won't help.

Caveat: it's been a while since I did any Java, and I've been doing C# since, so my memory could be somewhat muddled.
UberGerbil
Gerbil Khan
 
Posts: 9979
Joined: Thu Jun 19, 2003 3:11 pm

Postposted on Sat Jul 31, 2004 8:35 pm

Moved to the Developer's Den.

Kevin
Kevin
Administrator
 
Posts: 6592
Joined: Thu Dec 27, 2001 7:00 pm
Location: Minneapolis, MN


Return to Developer's Den

Who is online

Users browsing this forum: No registered users and 2 guests