-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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
IMBABOT
  • 55
  • 2
  • 13
  • 2
    Please explain the reason you don't want to use the `ArrayList`. – lexicore Apr 01 '18 at 11:32
  • I have not found any place where you're using it. – ikos23 Apr 01 '18 at 11:35
  • First, trying to undersand better how Arrays work in java, second target specification. – IMBABOT Apr 01 '18 at 11:36
  • ...and define "doesn't work." You seem to be using [Apache Commons' `ArrayUtils` class](https://commons.apache.org/proper/commons-lang/javadocs/api-3.7/org/apache/commons/lang3/ArrayUtils.html), which does work. – T.J. Crowder Apr 01 '18 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 [mcve]? – Bernhard Barker Apr 01 '18 at 11:38
  • it should work similar sprites = (Sprite[]) ArrayUtils.removeElement(sprites, i); array = ArrayUtils.removeElement(array, element); but it won't, reason I ask question – IMBABOT Apr 01 '18 at 11:39
  • `i < damageSprites.length` - shouldn't that be `sprites.length`? – Bernhard Barker Apr 01 '18 at 11:43
  • yes it should, look like this for (int i = 0; i – IMBABOT Apr 01 '18 at 11:47

2 Answers2

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.

Sean
  • 212
  • 1
  • 10
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);
IMBABOT
  • 55
  • 2
  • 13