【问题标题】:Positions in ArrayListsArrayLists 中的位置
【发布时间】:2023-03-24 01:47:01
【问题描述】:

我的代码没有像我想象的那样工作。列表listColumn0 包含屏幕上 8 个精灵对象的 X 和 Y 位置。当我触摸其中一个时,我会检查与 X 和 Y 位置匹配的女巫精灵对象,然后将其从列表中删除。但奇怪的是,这仅在我首先触摸索引为 7 的最后一个精灵对象,然后继续使用索引为 6 的精灵对象时才有效,依此类推。

如果我单击索引为 3 的精灵对象或除最后一个以外的其他对象,那么应用程序将关闭!为什么这个?有人可以看到我做错了什么,或者我可以以更好的方式做到这一点吗?有没有更好的方法来检测/匹配我触摸过的精灵对象?

        String size = Integer.toString(listColumn0.size());
    // Check all lists
    for(ColorObject colorObject: listColumn0) {
        if(x > (colorObject.xPosition - colorObject.radius) && x < (colorObject.xPosition + colorObject.radius) && y > (colorObject.yPosition - colorObject.radius) && y < (colorObject.yPosition + colorObject.radius)) {

            String colorCode = Integer.toString(colorObject.color);
            String index = Integer.toString(listColumn0.indexOf(colorObject));
            Log.i("Test","Match!! " + size + " Color: " + colorCode + "ID: " + index);

            listColumn0.remove(listColumn0.indexOf(colorObject));
        }
    }

编辑:

来自 LogCat 的错误消息:

05-22 07:08:55.482: W/dalvikvm(1444): threadid=12: thread exiting with uncaught exception (group=0x40a13300)
05-22 07:08:55.482: E/AndroidRuntime(1444): FATAL EXCEPTION: Thread-124
05-22 07:08:55.482: E/AndroidRuntime(1444): java.util.ConcurrentModificationException
05-22 07:08:55.482: E/AndroidRuntime(1444):     at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:569)
05-22 07:08:55.482: E/AndroidRuntime(1444):     at com.test.game.ColorObjectManager.checkPosition(ColorObjectManager.java:164)
05-22 07:08:55.482: E/AndroidRuntime(1444):     at com.test.game.GameLoop.run(GameLoop.java:190)
05-22 07:08:55.482: E/AndroidRuntime(1444):     at java.lang.Thread.run(Thread.java:856)
05-22 07:13:55.753: I/Process(1444): Sending signal. PID: 1444 SIG: 9

【问题讨论】:

  • 展开“应用程序正在关闭”。您收到任何错误消息吗?堆栈痕迹? ANR 对话框?

标签: android


【解决方案1】:

您不能在使用 foreach 循环迭代 listColumn0 时对其进行修改。这样做会产生ConcurrentModificationException,您可以在 LogCat 中看到它。

如果您使用老式的Iterator,您可以在迭代集合时修改它:

Iterator<ColorObject> it = listColumn0.iterator();
while(it.hasNext()) {
   ColorObject colorObject = it.next();
   ...
   it.remove(); // this removes the current object
}

为了缩小it 的范围,最好在此处使用 for 循环:

for (Iterator<ColorObject> it = listColumn0.iterator(); it.hasNext();) {
   ColorObject colorObject = it.next();
   ...
   it.remove(); // this removes the current object
}

【讨论】:

  • 谢谢!当你写老式的迭代器时,我想到了这个: for(int i = 0; i
  • @3D-kreativ 是什么让你觉得这更慢?
  • 不记得在哪里,但我在某处读到了有关性能的信息,我的第一个选择应该更快。
  • @3D-kreativ 我发现了一个关于这个的 SO 问题:stackoverflow.com/questions/2113216/…
  • 好的,感谢您的链接,感谢您的帮助!我想我的问题现在已经解决了。
【解决方案2】:

最简单的解决办法是

Integer colorObjectIndex = -1;
for(..)
.....
   colorObjectIndex = listColumn0.indexOf(colorObject)
.....
}
// check for colorObjectIndex > -1
listColumn0.indexOf(colorObjectIndex);

否则你必须使用 listColumn0 的迭代器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-03
    • 1970-01-01
    • 2013-03-22
    相关资源
    最近更新 更多