removing an element from an array

Discussion in 'Java' started by laredotornado, Oct 20, 2008.

  1. 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
     
    laredotornado, Oct 20, 2008
    #1
    1. Advertising

  2. laredotornado

    Alan Morgan Guest

    In article <>,
    laredotornado <> 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
     
    Alan Morgan, Oct 21, 2008
    #2
    1. Advertising

  3. laredotornado

    Guest

    On Oct 21, 4:18 am, (Alan Morgan) wrote:
    > In article <..com>,
    >
    > laredotornado  <> 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>
     
    , Oct 21, 2008
    #3
  4. laredotornado <> 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.
     
    Andreas Leitgeb, Oct 21, 2008
    #4
  5. On Oct 21, 2:18 am, wrote:
    > On Oct 21, 4:18 am, (Alan Morgan) wrote:
    >
    >
    >
    >
    >
    > > In article <>,

    >
    > >laredotornado <> 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
     
    laredotornado, Oct 21, 2008
    #5
  6. laredotornado

    Arne Vajhøj Guest

    wrote:
    > On Oct 21, 4:18 am, (Alan Morgan) wrote:
    >> In article <>,
    >> laredotornado <> 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
     
    Arne Vajhøj, Oct 26, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris  Chiasson
    Replies:
    6
    Views:
    652
    Richard Tobin
    Nov 14, 2006
  2. HANM
    Replies:
    2
    Views:
    762
    Joseph Kesselman
    Jan 29, 2008
  3. Larsenmtl Larsenmtl

    Array Of Objects, Removing Element From Memory

    Larsenmtl Larsenmtl, Aug 7, 2008, in forum: Ruby
    Replies:
    3
    Views:
    143
    Larsenmtl Larsenmtl
    Aug 7, 2008
  4. Robert TV

    Difficulty removing element from array

    Robert TV, Sep 21, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    110
    Eric J. Roode
    Sep 21, 2003
  5. Replies:
    4
    Views:
    109
    Michael Winter
    Mar 5, 2005
Loading...

Share This Page