1

I'm trying to figure out if there's someway for me to dynamically fill an array of objects within a class, without using array initialization. I'd really like to avoid filling the array line by line. Is this possible given the code I have here?

final class Attributes {

    private final int TOTAL_ATTRIBUTES = 7;

    Attribute agility;
    Attribute endurance;
    Attribute intelligence;
    Attribute intuition;
    Attribute luck;
    Attribute speed;
    Attribute strength;

    private Attributes[] attributes; //array to hold objects

    private boolean initialized = false;

    public Attributes() {
        initializeAttributes();
        initialized = true;

        store(); //method for storing objects once they've been initialized.

    }

    private void initializeAttributes() {
        if (initialized == false) {
            agility = new Agility();
            endurance = new Endurance();
            intelligence = new Intelligence();
            intuition = new Intuition();
            luck = new Luck();
            speed = new Speed();
            strength = new Strength();
        }
    }

    private void store() {
        //Dynamically fill "attributes" array here, without filling each element line by line.
    }
}
2
  • Just to clarify.. "You want to add all member variables typed Attributes to an array?" this is what you mean by dynamic rt? Commented Jun 23, 2011 at 18:01
  • I think you want to implement yourself whatever is already implemented in Java Collection, you could take a look at OpenJDK and see how they have done it. Commented Jun 23, 2011 at 18:06

4 Answers 4

3
attributes = new Attributes[sizeOfInput];

for (int i=0; i<sizeOfInput; i++) {
    attributes[i] = itemList[i];
}

Also, FYI you can add things to an ArrayList and then call toArray() to get an Array of the object.

Sign up to request clarification or add additional context in comments.

Comments

1

There is a short Array initialization syntax:

attributes = new Attribute[]{luck,speed,strength,etc};

1 Comment

Yeah, I was thinking about that, but I'm looking for something much more dynamic.
0
 Field[] fields =  getClass().getDeclaredFields();
 ArrayList<Attrubute> attributesList = new ArrayList<Attrubute>();
 for(Field f : fields)
 {
     if(f.getType() == Attrubute.class)
     {
         attributesList.add((Attrubute) f.get(this));
     }
 }
 attributes = attributesList.toArray(new Attrubute[0]);

4 Comments

while Reflection "solves" the problem here, it is more likely a design problem here :) And using getDeclaredFields might make it work better against non-public fields.
@mihi I can't comment on the design but using getDeclaredFields() does make sense.
one thing I don't understand is..why don't I just use the array to transfer the values, rather than the ArrayList?
@Holland array cannot grow dynamically like an ArrayList can. I you are going to have fixed number of attributes, you can declare an array of that size and use it.
-1

You can use a HashMap<String,Attribute>

3 Comments

How would a HashMap help in this case?
Everyone else has code samples, maybe you could throw one in too
This is really a comment, not an answer to the question. Please use "add comment" to leave feedback for the author.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.