Hey all, I'm trying to implement my half-understanding of arraylists, now that I've finished the chapter in Head First... The goal is to create 10 'mobs' (monsters) with random health and then be able to access those objects.
1. I have a class called MobInfo which is the attribute template for all monsters, it also includes a prototype of a 'spawner,' which gives them random health and a corresponding name. 2. I have a class called TestMobInfo which creates an MobInfo arraylist, then fills it with ten MobInfo objects which are created using the spawner. It also creates a MobInfo object which represents the player. It then (in theory) invokes the Attack class using the player MobInfo and the monster MobInfo, until all the monster MobInfos are dead and removed from the array. 3. The attack class is not included, because that part of the program doesn't even come into account. It doesn't come into account due to the fact that I can't for the life of me figure out how to use my new array of MobInfos. I tried everything from Mobs to Mobs(NewMob.5) etc. etc. etc.
EDIT: By utilizing a for loop such as the one below, I can access the objects directly and print values such as their names. Are there other ways I should know about at this point in my Java education? More importantly, am I still circling this concept as opposed to wrapping my mind around it?:
Here's the code, if it gives any context: MobInfo.class:
[ May 26, 2008: Message edited by: Alex Birmingham ]
[ May 26, 2008: Message edited by: Alex Birmingham ] [ May 26, 2008: Message edited by: Alex Birmingham ]
Lots more about ArrayLists in the Java Tutorial. Always worthwhile searching the tutorial, there is so much in it.
Joined: Oct 13, 2005
I would suggest you get some simple objects and put them into an ArrayList. It makes it much easier if you don't have to worry about monsters. Then when you know how the ArrayList works . . .You can add 1 in Java 5 because the int is automatically "boxed" into an Integer object. then try all sorts of other things with the ArrayList, and see how it works.
Joined: May 22, 2008
Thanks for the replies. I definitely need to go back and do this with more elementary objects. For now, though, I think I got it to work reasonably well. Here is the 'working' code if anyone is curious:
(The only major problem is that as the mob loses health, it still displays in the attack message a full value of health. But the value is definitely going down, because the mob eventually dies to a hit that is far less than a full value of health. Hrmmmmmm.)
Joined: Oct 13, 2005
Probably better to declare your ArrayList as List<Mob> rather than ArrayList. That will allow you to change the implementation, and still use all the methods of the List interface.
I think there is one method (might be mistaken) in ArrayList which isn't in List, the one which ensures capacity. You would have to call it like thisIf you have increased the capacity it becomes faster to add large numbers of elements.
Oh no, I have looked in the API and there is also a non-List method which "trims" the size of the ArrayList.