• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Conceptual Misunderstanding of Arraylists?

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I just had the craziest dream. This tiny ad was in it.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic