Personal computing discussed

Moderators: SecretSquirrel, just brew it!

Darth Gerbil
Topic Author
Posts: 7510
Joined: Sat Apr 24, 2004 7:53 pm
Location: the abyss into which you gaze

new Java problem: storing class instances

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...
Grand Gerbil Poohbah
Posts: 3577
Joined: Sun Feb 16, 2003 3:43 pm
Location: MD

Sat Jul 31, 2004 7:37 pm

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

return (FileMonitor) v.elementAt( index );
Grand Admiral Gerbil
Posts: 10259
Joined: Thu Jun 19, 2003 3:11 pm

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.
Posts: 6581
Joined: Thu Dec 27, 2001 7:00 pm
Location: Minneapolis, MN

Sat Jul 31, 2004 8:35 pm

Moved to the Developer's Den.


Who is online

Users browsing this forum: No registered users and 2 guests