Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7510135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:08:24+00:00 2026-05-29T23:08:24+00:00

I have an ArrayList which holds Planes (enemies) on my android game. These planes

  • 0

I have an ArrayList which holds Planes (enemies) on my android game. These planes move from one side of the screen to the other and the user has to dodge them. When a plane’s x value becomes less than -50 it is removed from the ArrayList. When this happens, it causes all the planes on the screen currently to ‘jump’ slightly. They disappear for a few milliseconds and then are re-drawn but 2px behind where they are supposed to be.

Here is the paint method, where planes is the ArrayList

public void onDraw(){
        bg1.onDraw(c);
    bg2.onDraw(c);
    chopper.onDraw(c);

    score.onDraw(c);

    // PAINTS THE PLANE OR DELETES IF OFF SCREEN
    for (int i = 0; i < planes.size(); i++) {
        Plane p = planes.get(i);
                    if(p.getX()<-50){
                            planes.remove(p);
                    }else{
                            p.onDraw(c);

                            if (p.getX() < 170) {
                                    detectPlaneCollision(p, c);
                            }
                    }

           }
}

Is there a way of fixing this? Should I use a different data structure?

Thanks

Tom

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T23:08:25+00:00Added an answer on May 29, 2026 at 11:08 pm

    I think your problem is nothing to do with your choice of data structure, but instead because you are modifying your List while you’re looping over it.

    Imagine you have three planes in your list – [P1,P2,P3].

    • On the first iteration of your loop i is 0, you process P1
    • P1 has getX() < -50 so you remove it, making the list now [P2,P3]
    • On the next iteration of the loop i is now 1 so you process P3.
    • This means P2 never gets processed and so will never be drawn, making it disappear briefly.

    Try using an Iterator which will allow you to safely remove items while looping over the List.

    Iterator<Plane> i = planes.iterator();
    
    while (i.hasNext()) {
      Plane p = i.next();
    
      if (p.getX() < -50) {
        i.remove();
      } else {
        p.onDraw(c);
    
        if (p.getX() < 170) {
          detectPlaneCollision(p, c);
        }
      }
    }
    

    Although, since you don’t care about the order in which you process your Plane obejcts you could consider storing them in a Set rather than a List. A Set doesn’t have to worry about maintaining an order of items as you add and remove them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ArrayList that contains Strings and also contains other ArrayLists which may
I have an arraylist which has a class datastructure. public class empRec { public
Ok, so I have an ArrayList (arrBok) , which is full of book objects
I have a ArrayList made up of different elements imported from a db, made
I have an ArrayList<String> , and I want to remove repeated strings from it.
I have an arraylist which I'd like to display in a tabular fashion, but
I have a JSP page where I load an arraylist which has the list
I have different objects which is used to store the data from DB in
I have an arraylist which has data structures in it. I am having problems
I have the arraylist which has some process names like Notepad, mspaint I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.