laredotornado said:
I'm using Java 1.5. Let's say I have an Object[] array. If I know a
valid index in that array, what is the easiest way to remove an
element at that index and get a corresponding Object[] array without
that element?
As others already suggested, an ArrayList (or other predefined
Collection classes) might be just what you really need/want.
It's surely the most convenient to use and the only feasible
way, if you do not want to impose any deliberate bound on the
size.
But there is also a way to do something like it with plain
arrays. Under special circumstances, it may be even faster:
If you know an upper bound to the number of elements, and
maintain your own "numElements", and if you do not care about
ordering of the elements, then deleting an element becomes as
simple as doing:
arr[pos]=arr[--numElements]; arr[numElements]=null;
and adding would be:
arr[numElements++]=newItem;
which on overflow would throw an ArrayIndexOutOfBoundsException.
Ripping off a correctly sized array from your arr/numElements
is a bit awkward, since Arrays.copyOf(...) is not yet available
in 1.5.