Anyone know how to remove an object at a particular index from an array? Im doing a Noah's Ark project for my class and Im using an Array to hold the abstract animal class and once an animal dies, I have to remove it from the list. However I can't seem to get it right. Any help would be nice :)

Dani AI

Generated

For 's Noah's Ark scenario: two practical approaches are safest — use a resizable collection so removals are trivial, or if sticking with a raw array, perform a controlled copy/shift and clean up the freed slot.

A compact, correct List-based approach (recommended for most cases):

List<Animal> herd = new ArrayList<>();
// add animals...
herd.remove(index);          // remove by index
// or remove while iterating safely:
for (Iterator<Animal> it = herd.iterator(); it.hasNext(); ) {
    if (it.next().isDead()) it.remove();
}

This handles resizing automatically and avoids manual index juggling.

If an array must be used, prefer System.arraycopy (fast, avoids per-element loops) and always null the vacated slot to allow GC:

// remove element at removeIndex from Animal[] animals in-place
int numMoved = animals.length - removeIndex - 1;
if (numMoved > 0) {
    System.arraycopy(animals, removeIndex + 1, animals, removeIndex, numMoved);
}
animals[animals.length - 1] = null; // avoid memory leak

To actually shrink the array (arrays are fixed-length), allocate a new smaller array and copy the two segments into it:

Animal[] smaller = new Animal[animals.length - 1];
System.arraycopy(animals, 0, smaller, 0, removeIndex);
System.arraycopy(animals, removeIndex + 1, smaller, removeIndex, animals.length - removeIndex - 1);
animals = smaller;

On concurrency: suggested Vector and noted thread-related reasons; warned about legacy usage. Vector is synchronized per-method but is generally superseded by modern options — use Collections.synchronizedList, CopyOnWriteArrayList, or other java.util.concurrent collections depending on read/write patterns and iteration needs.

Quick tips: when removing multiple elements during an update loop, iterate backwards or use an Iterator.remove() to avoid skipping elements or concurrent-modification issues.

Recommended Answers

All 6 Replies

you mean if you had the following:

intArray[0] = 0;
intArray[1] = 1;
intArray[2] = 2;
intArray[3] = 3;
intArray[4] = 4;

and you wish to convert it to:

intArray[0] = 0;
intArray[1] = 1;
intArray[2] = 3;
intArray[3] = 4;

you could use a for loop starting at i=3 and assign intArray = intArray[i+1], until i<intArray.length-1
but that still raises the problem of what do we assign intArray[4] to equal? I would definatly go with a java.util.ArrayList<E> as this is exactly what you seem to be looking for a resizeable array list. With a prebuilt delete method.

or you could use vectors

Use an ArrayList

don't use Vector, it's a legacy class that should have been deprecated years ago.

don't use Vector, it's a legacy class that should have been deprecated years ago.

java.util is a legacy package, how do you figure Vectors should have been deprecated? It is part of the collections framework, they also updated it for java 1.5 to support enumerated types

Still using Vectors - because of some things related to threads.
And it is more suitable than others.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.