-1

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?

8
  • 2
    Please explain the reason you don't want to use the ArrayList.
    – lexicore
    Commented Apr 1, 2018 at 11:32
  • I have not found any place where you're using it.
    – ikos23
    Commented Apr 1, 2018 at 11:35
  • First, trying to undersand better how Arrays work in java, second target specification.
    – IMBABOT
    Commented Apr 1, 2018 at 11:36
  • ...and define "doesn't work." You seem to be using Apache Commons' ArrayUtils class, which does work. Commented Apr 1, 2018 at 11:37
  • The remove method you're using should be working (apart from the fact that you're going to have some problems with your index if you want to remove consecutive elements), so the problem is likely somewhere else. Can you post a minimal reproducible example? Commented Apr 1, 2018 at 11:38

2 Answers 2

0

Like everyone else, I'd recommend a List for most use cases, but you've given reasons for not using one.

I'm not sure why ArrayUtils.removeElement isn't working for you, but since you're wanting to learn about arrays, I'll explain how this would be done without a helper method:

int indexToRemove = 4; //the index    we're removing
Object[] newArray = new Object[array.length-1];
for(int i=0; i<array.length;i++){
     if(i<indexToRemove){
        newArray[i] = array[i];
     }
     else if(i>indexToRemove){
        newArray[i-1] = array[i];
     }
}

This loops through the original array, copying each item over to a new array. When it hits the deleted index, it skips a copy, then continues from the next index, adjusting the indices by -1 so that they match up correctly.

0

work like this:

   for (int i = 0; i <sprites.length ; i++) {
                if (sprites[i].halfHeight == 0 && sprites[i].halfWidth == 0){
           sprites = (Sprite[]) ArrayUtils.remove(sprites, i);
      }

just change:

ArrayUtils.removeElement(sprites, i);

to:

ArrayUtils.remove(sprites, i);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.