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 :)

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

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.