2

Is it a "good idea" to use an arraylist to iterate through a group of items in a game?

For example if I have 5 "enemy objects" is it wiser to use this:

for (int e = 0; e < maxenemy; e++)
{
Enemy[e].Update();
}

or can I use this with comprable results?

for(Enemy e : enemies)
{
Enemy.get(e).Update();
}

I'm trying to decide if using the ArrayList is a viable option instead of having a static array of "enemy objects"

Mike L.
  • 589
  • 6
  • 16
GideonKain
  • 744
  • 1
  • 12
  • 27
  • [Array or List in Java. Which is faster?](http://stackoverflow.com/questions/716597/array-or-list-in-java-which-is-faster) That is pretty much the same question... – Knickedi Oct 26 '11 at 00:21
  • 1
    If somebody's answer solved your problem you should accept their answer by clicking the right mark beside the answer. Or else, people will not consider helping you in the future. – 500865 Oct 28 '11 at 01:41

5 Answers5

0

The main difference between an Array and an ArrayList is that an Array is fixed size whereas an ArrayList is not and can continually grow. So if you know that there will always only be 5 enemy objects then stick with an Array, however if the number of enemy objects grows then use an ArrayList

EDIT:

Your ArrayList example is wrong. It should be:

for(Enemy e : enemies)
{
 e.Update();
}

When you iterate through an ArrayList like this, e becomes an actual Enemy object from the enemies list. So you can just call Enemy methods on e.

Mike L.
  • 589
  • 6
  • 16
  • thanks, what got me was the "get" part, as in it seemed like just a "repository" that I should only be using to store and retrieve instead of actively using - am I correct in assuming that it is usable like a regular array? – GideonKain Oct 26 '11 at 00:21
  • Yes you can stick get an item out of the ArrayList by saying list.get(index) and passing in the index you would like to get. – Mike L. Oct 26 '11 at 00:22
0

If you have fixed amount of enemies and you always just iterate through them, then array will be bit quicker. However ArrayList is much more flexible if you need to modify the list or do more complex queries.

Lycha
  • 9,937
  • 3
  • 38
  • 43
0

Well, an array list would probably be better if your game gains or loses enemies during its lifetime. A static array will be fixed and not very flexible. I also noticed your second code snippet is probably better written as

for(Enemy e : enemies)
{
e.Update();
}

Simpler syntax, and perhaps a bit more efficient. :D

Zhanger
  • 1,290
  • 14
  • 19
0

The #2 way is good on all devices while #1 is good only on devices with JIT.

Look at the Use Enhanced For loop syntax section here

500865
  • 6,920
  • 7
  • 44
  • 87
0

You should use ArrayList instead of a naked array for many reasons:

  1. Although the naked array solution will be slightly faster, in almost all cases the difference will be negligible. Almost certainly the performance bottlenecks will be elsewhere.

  2. Using ArrayLists gives you the flexibility of using some other type of List or Iterable in future. You might for example later realize that you need to be able to dynamically add and remove enemies, but that you never need to index into the middle of the list -- in that case a linked list would be better.

  3. Compare the code of the two solutions:

int m = enemies.length; for (int i = 0; i < m; i++) enemies[i].update();

and

for (Enemy e : enemies) e.update();

The array version is much longer and therefore more likely to have bugs. Therefore to increase the code quality is better to use the ArrayList.

Eamonn O'Brien-Strain
  • 3,352
  • 1
  • 23
  • 33