I am trying to create a class to test my understanding, and it takes an ArrayList
as parameter. The decision of choosing an ArrayList
is because it does not have a fixed size and I can add more elements to it later.
I came out with this implementation:
public class Character {
private String name;
private char gender;
private List<String> inventory = new ArrayList<String>();
public Character(String name, char gender, List<String> inventory) {
this.name = name;
this.gender = gender;
this.inventory.addAll(inventory); // Is this a good idea?
}
public String getName() {
return name;
}
public char getGender() {
return gender;
}
public List<String> getInventory() {
return inventory;
}
}
And in the main class:
public class Main {
public static void main(String[] args) {
Character coolKid = new Character("Cool Kid", 'f',
Arrays.asList("just", "testing"));
System.out.println(coolKid.getInventory());
// returns [just, testing]
}
}
My main concern is "dynamically" adding elements to the ArrayList when instantiating, as well as using addAll in the constructor, and List as the type for the inventory argument in the constructor.
Is there a better way to accomplish this?