aspose file tools*
The moose likes Beginning Java and the fly likes Conceptual Misunderstanding of Arraylists? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Conceptual Misunderstanding of Arraylists?" Watch "Conceptual Misunderstanding of Arraylists?" New topic
Author

Conceptual Misunderstanding of Arraylists?

Alex Birmingham
Ranch Hand

Joined: May 22, 2008
Posts: 54
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 ]

- SCJA 1.0
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1347
    
  13
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

Joined: Oct 13, 2005
Posts: 36579
    
  16
Lots more about ArrayLists in the Java Tutorial. Always worthwhile searching the tutorial, there is so much in it.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
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

Joined: May 22, 2008
Posts: 54
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

Joined: Oct 13, 2005
Posts: 36579
    
  16
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Conceptual Misunderstanding of Arraylists?
 
Similar Threads
Swing lag
Stylistic Question
Commands to Objects
Returning Multiple Values
Please help me do what I'm doing...but better.