A
A
AlexAlex0002019-01-10 19:13:45
Java
AlexAlex000, 2019-01-10 19:13:45

How to compare object data in two ArrayList?

I am writing a small toy for android.
I had such a code that checked the collision of two objects (taken from one of the Habr articles). Asteroids (asteroid), which are stored in the Arraylist (Asteroids) and the spacecraft (ship).

for (Asteroid asteroid : asteroids)
            if (asteroid.isCollision(ship.x, ship.y, ship.size)) {
            }

later I wanted to add a collection of coins (coin), which means it was necessary to remove them when colliding with a ship. So I used an Iterator to remove the particular coin that collided with the ship. With this, too, there were no special problems.
for(Iterator<Coin> iterator = coins.iterator(); iterator.hasNext();) {
            Coin coin = iterator.next();
            if (coin.isCollision1(ship.x, ship.y, ship.size)) {
                iterator.remove();
            }
        }

But now I want that when a coin (coin) and an asteroid (asteroid) collide, both are removed. I understand that I need something like this:
for(Iterator<Coin> iterator = coins.iterator(); iterator.hasNext();) {
int x;
int y;
int size;
if (coin.isCollision1(x, y, size)) {
                iterator.remove();
                        /*а здесь какая то магия вызывающая итератор Asteroids и подставляющая координаты астероидов x,y,size для сравнения в int x, int y, int size. */
        }
}

But I have no idea how to do it. I honestly googled for a few days, but did not find anything on the topic.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Alexandrov, 2019-01-10
@AlexAlex000

In your case, everything is simple, but this is not entirely correct. the complexity increases in the calculations.
You just need a nested loop.
Loop asteroid_iterator asteroids {
loop coin_iterator coin {
if coin_iterator.colise(asteroid_iterator.coords) remove both
}
}
But again, this is not entirely correct and if there are a lot of objects, then it’s generally not applicable so on the forehead.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question