removing an element from an array

L

laredotornado

Hi,

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?

Thanks, - Dave
 
A

Alan Morgan

Hi,

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?

Not using an array would be my first suggestion. Have you considered
using an ArrayList and converting to an array if and when you need to?

Alan
 
K

kingalex239

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?

Not using an array would be my first suggestion.  Have you considered
using an ArrayList and converting to an array if and when you need to?

Alan

I agree with this. Have an arrayList and delete or add element
whenever you need and use ArrayList.toArray() to get the desired array
at any time.


================================================================
Community for the Java programmers to share views,ideas and post
queries <a href="www.techcubetalk.com"> www.techcubetalk.com </a>
 
A

Andreas Leitgeb

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.
 
L

laredotornado

laredotornado  said:
Hi,
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?
Not using an array would be my first suggestion.  Have you considered
using an ArrayList and converting to an array if and when you need to?

I agree with this. Have an arrayList and delete or add element
whenever you need and use ArrayList.toArray() to get the desired array
at any time.

================================================================
Community for the Java programmers to share views,ideas and post
queries <a href="www.techcubetalk.com">www.techcubetalk.com</a>- Hide quoted text -

- Show quoted text -

I am passing an Object[] to a method that I didn't write. So if you
know what the answer is in terms of ArrayList and you can convert it
back to Object[], I'm all for it. Just know that the conditions of
this situation are such that you start with an Object[] and have to
end with an Object[]. Anything you do in between is fine with me. -
Dave
 
A

Arne Vajhøj

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?
Not using an array would be my first suggestion. Have you considered
using an ArrayList and converting to an array if and when you need to?

I agree with this. Have an arrayList and delete or add element
whenever you need and use ArrayList.toArray() to get the desired array
at any time.

If the intention is to delete in the middle, then LinkedList seems
more obvious than ArrayList to me.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top