Jump to content
  • Advertisement
Sign in to follow this  
Halsafar

[java] A Vector class to contain an array of abstract units

This topic is 4661 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay I have - abstract class cMoveableObject - public class cPlayer extends cMoveableObject Now it seems the VECTOR class which comes with Java stores the OBJECT reference that all objects derive from... So once I store cPlayer instance in the array I can no longer work it... No longer access cPlayer or cMoveableObject... Any help?

Share this post


Link to post
Share on other sites
Advertisement
Never mind I figured it out...
Just making sure this is correct

Vector<cPlayer> v = new Vector<cPlayer>();

It also removes all the -Xlint errors ^_^
I shoulda read my earlier post's more carefully.

Share this post


Link to post
Share on other sites
Just FYI, the pre-generics way to do this is just to cast the Object to something more specific every time you take it out of the vector.

Like this:

cPlayer a_player = (cPlayer) vector.elementAt(n);

If vector.elementAt() returns something that's not a cPlayer, it will throw a ClassCastException. You can even catch the ClassCastException, if there's a chance that the object won't be the right class.

Share this post


Link to post
Share on other sites
As an addition to this topic, the newly added Generics are purely syntactic sugar. Only the compiler sees this. The byte code of the compiled class is the same as without generics. You can verify this by writing code that uses each and running javap on the compiled class.

Share this post


Link to post
Share on other sites
Yes, Whackjack is right.

For the OP, do you really need to use a Vector? Nothing wrong with it, except for the fact that a List implementation would give you better results, performance-wise. Try ArrayList, or even LinkedList, which is very famous on these forums, it has many fans ;D

Son Of Cain

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!