How to remove an object from an array without using ArrayList?
I try to create miniaplication using Swing. At this moment it contains the main window, and circles in it, that going around, and when I click on the circle - it disappears.
And when circle disappears it should be removed from the array. I don't understand how to do it.
Here is the code: Arrays with objects;
Sprite[] sprites = new Sprite[10];
Method to delete object:
private void deleteCircle(GameCanvas gameCanvas) {
gameCanvas.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
for (int i = 0; i <sprites.length ; i++) {
boolean takeDamage = x >= sprites[i].getLeft() && x<= sprites[i].getRight() && y >= sprites[i].getTop() && y <= sprites[i].getBottom();
if (takeDamage){
sprites[i].halfHeight = 0;
sprites[i].halfWidth = 0;
}
}
for (int i = 0; i <damageSprites.length ; i++) {
if (sprites[i].halfHeight == 0 && sprites[i].halfWidth == 0){
sprites = (Sprite[]) ArrayUtils.removeElement(sprites, i);
}
System.out.println(Arrays.toString(sprites));
}
}
});
}
if object.halfHeight = 0
and object.halfWidth = 0
it should be considered like it not exists, and should be removed from the array:
Try to remove it like this, but this doesn't work
for (int i = 0; i <damageSprites.length ; i++) {
if (sprites[i].halfHeight == 0 && sprites[i].halfWidth == 0){
sprites = (Sprite[]) ArrayUtils.removeElement(sprites, i);
How can I remove the object from Array without using ArrayList?
ArrayList
.ArrayUtils
class, which does work.