-1

I have a dynamic object array like this:

private Array<Block> blocks=new Array<Block>();

I am iterating this array like this and removing random elements according to a condition:

for (Block d : blocks) {
    if (b.checkBlockCollision(frame)) {
        blocks.removeValue(d, true);

    }

Only some of the elements that satisfies the condition are getting removed from the array.I want to take only these removed elements in to static array.

Is that really possible?

Niranjana
  • 514
  • 5
  • 23

1 Answers1

1

You shouldn't use forEach loop, if you remove elements inside the loop. Look at this question. That being said, you can do it like this for example:

private Array<Block> blocks = new Array<Block>();
private Block[] removedBlocks = new Block[4];

void someMethod() {
    //...

    // skip non-null removed blocks
    int j = 0;
    for (Block b : removedBlocks) {
        if (b != null) j++;
    }

    int i = 0;
    while (i < blocks.size) {
        if (blocks.get(i).checkBlockCollision(frame)) {
            //removing by index is faster, and it returns removed element, so you can do it like this
            removedBlocks[j++] = blocks.removeIndex(i);
            if (j == removedBlocks.length) {
                //game over
                break;
            }
        } else {
            i++;
        }
    }
}

I have to note, that although you can use non-dynamic array here, it would be much more convenient to use dynamic array. For example, you wouldn't have to check, how many non-null references in the array. And clearing a dynamic array takes one method call, when with non-dynamic array, you need to do it in a loop. Think about it, maybe you will want to change your approach.

Arctic45
  • 1,078
  • 1
  • 7
  • 17
  • I want to add removed values in to static array. – Niranjana Dec 14 '17 at 11:33
  • Instead of add(),what to use for static array? – Niranjana Dec 14 '17 at 11:39
  • And what should happen, when number of removed Blocks exceeds 4 ? – Arctic45 Dec 14 '17 at 12:05
  • I tried this.But when I print the removedBlocks array,only zeroth index contains the removed element.All other indices are null.Whenever a new removed element comes,it is occupying in zeroth index only.I want to insert the first four collided/removed elements sequentially. – Niranjana Dec 15 '17 at 07:00
  • Updated once more. – Arctic45 Dec 15 '17 at 07:51
  • Now this works fine.Thanks a lot!Shall I ask you one more thing?If I am using an array list instead of static array,how it differs?Further in my game,I want to check the order and type (since the Block array contain different colored blocks as subclass types)of elements stored in the removedBlocks array.If that is the case,which one is better to use?Dynamic or static? – Niranjana Dec 15 '17 at 09:04
  • Dynamic array can change size at runtime. You should use dynamic arrays, when number of elements in it can change. As for the second part of your question, you can use Libgdx `Array` class, it's ordered by defalut. – Arctic45 Dec 15 '17 at 09:12
  • Will do the same.Thanks! – Niranjana Dec 15 '17 at 09:13