Answer the question
In order to leave comments, you need to log in
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)) {
}
for(Iterator<Coin> iterator = coins.iterator(); iterator.hasNext();) {
Coin coin = iterator.next();
if (coin.isCollision1(ship.x, ship.y, ship.size)) {
iterator.remove();
}
}
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. */
}
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question