Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Conceptual Misunderstanding of Arraylists?

 
Alex Birmingham
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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[4] 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:


TestMobInfo.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 ]
 
Katrina Owen
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alex,

I think what you're looking for is



You might also need


For the full story on how to use and abuse the arraylist, I'd suggest taking a look at the API.

It can be a bit confusing at first, but once you get a handle on what you can find in the API docs, they are a real treasure trove!
 
Campbell Ritchie
Sheriff
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lots more about ArrayLists in the Java Tutorial. Always worthwhile searching the tutorial, there is so much in it.
 
Campbell Ritchie
Sheriff
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Alex Birmingham
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)

MobInfo.class:


Attack.class


TestMobInfo.Class:
 
Campbell Ritchie
Sheriff
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic