简体   繁体   English

从第一个开始到最后一个开始时出现迭代错误,即以正常方式

[英]Iteration error when started from 1st one to last one i.e in normal way

 @Override

   public boolean onTouchEvent(MotionEvent event) 
        {

         if (System.currentTimeMillis() - lastClick > 300) 
               {

                lastClick = System.currentTimeMillis();

                synchronized (getHolder()) {

                    for (int i = sprites.size() - 1; i >= 0; i--) <-------why in reverse?
                        { 

                        Sprite sprite = sprites.get(i); 

                        if (sprite.isCollition(event.getX(), event.getY())) 
                              { 

                              sprites.remove(sprite); 

                              break; 

                        } 

                    } 

                }

But when i iterate from lastone to firstone ie in reverse order it gives the result.why ..? 但是,当我从lastone迭代到firstone时,即以相反的顺序,它会给出结果。为什么..? Need help 需要帮忙

It's because you are removing the sprites while inside the for loop. 这是因为要在for循环中删除精灵。 When you alter the number of objects in the loop when moving forward, you stuff up the iteration. 向前移动时,如果更改循环中的对象数,则会填充迭代。 When you remove the current element, all elements found in the list after this element (and still to be enumerated through) are now shifted down an index, ie the element that was at index i+1 is now at index i . 当您删除当前元素时,在列表中找到的该元素之后的所有元素(并且仍要进行枚举)现在都下移一个索引,即位于索引i+1处的元素现在位于索引i

If you are removing from the loop you are iterating through, the correct way is to iterate in reverse, like you have found out. 如果要从循环中删除,则正确的方法是像发现的那样反向进行迭代。 That way when you remove the current element, all other elements to still be enumerated through are still at the same index. 这样,当您删除当前元素时,所有其他仍要枚举的元素仍位于同一索引处。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 最好的方式来只做一种方式,即服务沟通活动? - Best way to make only one way i.e activity to service communication? 我在片段中有2个RecyclerViews。 但只显示了第一个 - I have 2 RecyclerViews in a Fragment. But only the 1st one is shown How to move from one activity to another ie Java Class to Kotlin Class in an Android Java Project? - How to move from one activity to another i.e Java Class to Kotlin Class in an Android Java Project? 从第一个活动转到第二个活动时出错 - error when going from 1st activity to 2nd activity 当回收站视图位于 0 position 即尚未开始滚动时,在回收站视图中向下滑动 - Swipe down in recycler view when recycler view is at 0 position i.e has not started scrolling 启动新活动时,是否可以从活动中释放内存? - Is there a way to free memory from a activty when a new one is started? 将用户单击/滑动的项目从第一个片段的一个列表视图转移到第二个片段的另一个列表视图 - Transferring user clicked/swiped item from one listview in 1st fragment to another listview in 2nd fragment 尝试在我的应用程序中创建两个服务并从每个服务发送通知,但是第二个服务取代了第一个 - Trying to Create Two services in my application and send notifications from each, but the 2nd one replaces the 1st 仅在第一次启动时解析XML - Parsing XML one time only on 1st startup 在单个线程中处理多个无限任务? PS一次运行一个任务,并从外部控制其任务行为(即启动/停止任务) - handle multiple infinite tasks in a single thread? P.S run one task at a time and control its task behavior(i.e starting/stoping task) from outside
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM