android 使用ArrayList
使用数组列表迭代游戏中的一组项目是一个“好主意”吗?
例如,如果我有 5 个“敌人对象”,那么使用它是否更明智:
for (int e = 0; e < maxenemy; e++)
{
Enemy[e].Update();
}
或者我可以使用它来获得可比较的结果吗?
for(Enemy e : enemies)
{
Enemy.get(e).Update();
}
我正在尝试确定使用 ArrayList 是否是一个可行的选择,而不是使用“敌人对象”的静态数组
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"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果你有固定数量的敌人并且你总是只是迭代它们,那么数组会更快一些。但是,如果您需要修改列表或执行更复杂的查询,
ArrayList
会更加灵活。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.好吧,如果你的游戏在其生命周期内获得或失去敌人,数组列表可能会更好。静态数组是固定的,不太灵活。我还注意到您的第二个代码片段可能更好地编写为
更简单的语法,并且可能更有效一些。 :D
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
Simpler syntax, and perhaps a bit more efficient. :D
第 2 种方法适用于所有设备,而第 1 种方法仅适用于具有 JIT 的设备。
请参阅此处使用增强型 For 循环语法部分一个>
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
Array 和 ArrayList 之间的主要区别在于 Array 的大小是固定的,而 ArrayList 的大小不是固定的,并且可以不断增长。因此,如果您知道始终只有 5 个敌人对象,则坚持使用数组,但是如果敌人对象的数量增加,则使用 ArrayList
编辑:
您的 ArrayList 示例是错误的。它应该是:
当您像这样迭代 ArrayList 时,e 成为敌人列表中的实际 Enemy 对象。所以你可以直接调用 e 上的 Enemy 方法。
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:
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.
您应该使用 ArrayList 而不是裸数组,原因有很多:
尽管裸数组解决方案会稍微快一些,但在几乎所有情况下,差异都可以忽略不计。几乎可以肯定,性能瓶颈将在其他地方。
使用 ArrayLists 可以让您在将来灵活地使用其他类型的 List 或 Iterable。例如,您稍后可能会意识到您需要能够动态添加和删除敌人,但您永远不需要索引到列表的中间 - 在这种情况下,链接列表会更好。
比较两种方案的代码:
int m = girls.length; for (int i = 0; i < m; i++) 敌人[i].update();
和
for (Enemy e : 敌人) e.update();
数组版本更长,因此更有可能出现错误。因此要提高代码质量最好使用ArrayList。
You should use ArrayList instead of a naked array for many reasons:
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.
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.
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.