Note that there are some explanatory texts on larger screens.

plurals
  1. POC-like enum in Java
    text
    copied!<p>I'm trying to find a Java equivalent for the following convenience in C++:</p> <pre><code>enum { ANIMAL_CAT = 0, ANIMAL_RAT, ANIMAL_BAT, ... NUM_ANIMALS }; Animal animals[NUM_ANIMALS]; animals[ANIMAL_CAT].mNumLegs = 4; animals[ANIMAL_RAT].mNumLegs = 4; ... </code></pre> <p>I know this isn't the prettiest thing in the world, but I can add a new ANIMAL_xxx anywhere in the enum and all the following entries will be automatically adjusted. Is there a clean way to do this in Java?</p> <hr> <p>Thanks for the responses, but I may have alluded to a greater simplicity than I intended.</p> <p>I'm working on a game, where there's a physics, AI engine, etc. I'm trying to consolidate all the information I need for each type of entity (some have only a physical manifestation, while some have both phys and AI, etc...), so that I can easily add/modify types (so, animal is a bad example, because I may need rocks, plants, etc...).</p> <p>I'm using a class to store the type info, while other classes rely on that class to look up the attributes (dimensions, bitmap index, hasAi, etc...) In the end, I'm trying to clean up something similar to the following, while allowing me to easily add new types:</p> <pre><code>class UnitTypeInfo { UnitTypeInfo() { mTypes = new TypeInfo[NUM_TYPES]; // and allocate... // initialize TypeInfos here mTypes[TYPE_CAT].mType = TYPE_CAT; mTypes[TYPE_CAT].mMass = 10.0f; mTypes[TYPE_CAT] ... mTypes[TYPE_ROCK].mType = TYPE_ROCK; ... } public class TypeInfo { public int mType; public int mRawResourceHandle; public float mMass; public Vector2d mDimensions; public float mHealth; public boolean mHasAi; ... } public static final int TYPE_CAT = 0; public static final int TYPE_ROCK = 1; public static final int TYPE_TREE = 2; ... public static final int NUM_TYPES = ???; // the last TYPE_ + 1 public TypeInfo mTypes[]; } </code></pre> <p>Now, this seems like some sort of XML implementation might be the 'right' thing to do, but I'm not sure about how to do that (new to Java). Anyhow, the other classes can easily just use unitTypeInfo.mTypes[UnitTypeInfo.TYPE_CAT].mMass (instantiated) to look up the cat's mass. However, if I want to add a TYPE_DOG underneath the TYPE_CAT definition, I have to update everything underneath it (lazy, I know, but let's figure there are a few more types.)</p> <p>Enums offer an easy solution to this problem in C++, but in Java, the simplest solution I can can up with now requires something like: unitTypeInfo.mTypes[UnitTypeInfo.mTypeEnum.TYPE_CAT.ordinal()].mMass - and while I suppose this isn't <em>much</em> worse than my already crummy solution, it does add a fair bit more indirection and an actual method call (to a method that most sources do not seem to encourage).</p> <p>One more thing is that I want to be able to call a switch(type), so that might also limit the possible solutions.</p> <p>Anyhow, I'm starting to get the idea that there must be a much better solution to this whole problem. Any suggestions?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload