Al Davis wrote:I don't think I understand your answer. At present my vamps list is declared and resides in another class; thus the call:
vamp1 is the object used to access the add method in the class Vamp.
This isn't a big deal, actually. There's no reason I can't simply write out 5 "new Vamp" statements, each giving a new name and new coordinates. But 1) I'm trying to learn as much as I can and 2) I'm trying to be slick!
Additionally, the reference to "vamp1" only lives inside the loop so that each time around the loop, and when the loop ends, all instances of "vamp1" will be garbage collected, i.e. will disappear.Carey Brown wrote:So, you create a new Vamp, which has-a list of Vamps, and which adds itself to the List. The result is that no matter how many Vamps you create, each List in the Vamp will only ever contain one Vamp, which is itself.
Carey Brown wrote:You will need a List of Vamp declared outside of the loop. Then each time around the loop create a new Vamp and add it to the list. If you need to make sure that no two Vamps have the same coordinate you'll have to add the logic for that.
The problem is that you don't have -a- list, you have -a-bunch-of- lists.Is creating the List in a separate class not sufficient?
Carey Brown wrote:
The problem is that you don't have -a- list, you have -a-bunch-of- lists.Is creating the List in a separate class not sufficient?
You haven't defined a member variable for Vamp called "name", so there is no concept of "name" for a Vamp. If you mean "vamp1" or my "v", those are variable names and not the name of the Vamp. Somebody needs to own the list of Vamps, a single list, not multiple lists. You could make the list a static member of Vamp, meaning, only one list will exist regardless of how many Vamp instances are created. Or, you could have your Dungeon instance own the list of Vamps. It's not readily appearent which is a better approach at this point. Either way the list would be created ouside of the loop.Al Davis wrote:Because I have defined the ArrayList in the Vamp class, I need to use the call:
to add vamp1 to the vamps ArrayList. Using your code, I would be adding 5 instances of Vamp V to the ArrayList, all with the same name. Will that work? Would using a LinkedList give me a better ability to keep track of the vamps? Does that really matter since they're only created once and will be differentiable based on their X.Y values? Is it the getVamps() method in my code which is screwing things up, or something else?
As you can see, I really struggle with the design aspects of Java in general, and the inheritability/interface/abstraction issues in particular. The problems are related, but I'm not sure how.
Thanks for your help, and your patience.
Al Davis wrote:Because I have defined the ArrayList in the Vamp class, I need to use the call:
Al Davis wrote:Should Lists, objects, etc be created within the class they pertain to, or should they be created elsewhere?
Don't get me started about those stupid light bulbs. |