Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > removing an element from an array

Reply
Thread Tools

removing an element from an array

 
 
laredotornado
Guest
Posts: n/a
 
      10-20-2008
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
 
Reply With Quote
 
 
 
 
Alan Morgan
Guest
Posts: n/a
 
      10-20-2008
In article <(E-Mail Removed)>,
laredotornado <(E-Mail Removed)> wrote:
>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
--
Defendit numerus
 
Reply With Quote
 
 
 
 
kingalex239@gmail.com
Guest
Posts: n/a
 
      10-21-2008
On Oct 21, 4:18*am, (E-Mail Removed) (Alan Morgan) wrote:
> In article <(E-Mail Removed)..com>,
>
> laredotornado *<(E-Mail Removed)> wrote:
> >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
> --
> Defendit numerus


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>
 
Reply With Quote
 
Andreas Leitgeb
Guest
Posts: n/a
 
      10-21-2008
laredotornado <(E-Mail Removed)> wrote:
> 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.

 
Reply With Quote
 
laredotornado
Guest
Posts: n/a
 
      10-21-2008
On Oct 21, 2:18*am, (E-Mail Removed) wrote:
> On Oct 21, 4:18*am, (E-Mail Removed) (Alan Morgan) wrote:
>
>
>
>
>
> > In article <(E-Mail Removed)>,

>
> >laredotornado*<(E-Mail Removed)> wrote:
> > >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
> > --
> > Defendit numerus

>
> 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
 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      10-26-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> On Oct 21, 4:18 am, (E-Mail Removed) (Alan Morgan) wrote:
>> In article <(E-Mail Removed)>,
>> laredotornado <(E-Mail Removed)> wrote:
>>> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Array Of Objects, Removing Element From Memory Larsenmtl Larsenmtl Ruby 3 08-07-2008 06:16 PM
Once again pointer to first element vs pointer to array cast topointer to element Szabolcs Borsanyi C Programming 6 05-23-2008 11:06 AM
how to Update/insert an xml element's text----> (<element>text</element>) HANM XML 2 01-29-2008 03:31 PM
Removing an element of an array effendi@epitome.com.sg Javascript 4 03-05-2005 05:13 PM
Difficulty removing element from array Robert TV Perl Misc 3 09-21-2003 02:37 PM



Advertisments