Really basic OO comprehension issue I am running into, any help is greatly appreciated.
I'm trying to add instances of "Thing" to an arraylist every-time I press a button, I can't wrap my head around how to create unique instances to add to the list. A different button press should remove the most recent object from the list.
ArrayList myList = new ArrayList<Thing>();
if(input.isKeyPressed(Input.KEY_A)){
Thing myThing = new Thing();
myThing.setNumber(myList.size());
myList.add(myThing);
}
if(input.isKeyPressed(Input.KEY_R)){
if(myList.size()>0){
myList.remove(myList.size()-1);
}
}
If I plan on making lots of "things" and I don't care about what they are called (nor do I want to keep track of unique thing-object names). How can I create a unique 'thing' object on each button press with minimal pain.
UPDATE: Thanks for the comments, please let me try to articulate my question better... When I create an ArrayList full of 'Thing', each instance of which is called "myThing", every instance has the same instance variables values.
If I wanted some of the 'Thing''s to have boolean isVisable = true
, and other's to have boolean isVisable = false
. I get stuck because each element of the list has the same name.