Deleting an element in an array..

Discussion in 'Java' started by justineee, Oct 26, 2008.

  1. justineee

    justineee Guest

    Hi Everyone,

    I am making a project that runs like a word storage. The user inputs
    words and stores it in arrays. Also, the user can delete words and
    display the new collection of words. However, I am having problems in
    deleting an element in an array.

    Here is my code.

    if (jnd.getSource()==deleteButton && (!(inputDelete.equals(""))))
    {

    getInputDelete = inputDelete.getText();
    int dCtr;
    List<String> arrayWordsCopy = Arrays.asList(arrayWords);
    for (dCtr = 0; dCtr < arrayWords.length; dCtr++)
    {
    if (getInputDelete.equalsIgnoreCase(arrayWords[dCtr]))
    {
    arrayWordsCopy.remove(dCtr);
    textArea.append("The word "+getInputDelete+" has been deleted in
    the dictionary.");
    }
    else
    {
    }
    }
    arrayWordsCopy.toArray(arrayWords);
    }

    I tried to convert the array to List temporarily so I can use the
    ".remove" method. Then I will get it back to an array so the element
    will be deleted permanently. There is no error in this code when I
    compile it but when I run the program and I press the button
    "deleteButton", there is a run-time error the occurs.

    Can anyone please help me to delete an element in an array?
    justineee, Oct 26, 2008
    #1
    1. Advertising

  2. On Sun, 26 Oct 2008 05:48:10 -0700, justineee wrote:

    > I am making a project that runs like a word storage. The user inputs
    > words and stores it in arrays. Also, the user can delete words and
    > display the new collection of words. However, I am having problems in
    > deleting an element in an array.
    >

    Why are you comparing against the array instead of the List you just
    built?

    More to the point, why bother with the complexity and overheads of
    continually converting a array to a List and back? Why aren't you using
    an ArrayList or TreeSet instead? ArrayList lets you control ordering and
    duplicates while TreeSet will do both for you. Both provide add/remove/
    existence tests.


    --
    martin@ | Martin Gregorie
    gregorie. | Essex, UK
    org |
    Martin Gregorie, Oct 26, 2008
    #2
    1. Advertising

  3. Lew <> wrote:
    > To delete element 'kx' (>= 0 and less than the current number of active
    > elements) from an array 'stuff' that initially holds 'stuffLength' elements:


    > --stuffLength;
    > for ( int jx = kx; jx < stuffLength; ++jx )
    > {
    > stuff [jx] = stuff [jx+1];
    > }
    > stuff [stuffLength] = null;
    > Pretty straightforward, yes?


    In case ordering doesn't matter, there's also this simpler approach:
    stuff[kx]=stuff[--stuffLength]; stuff[stuffLength]=null;

    Anyway, using java.util.ArrayList (or any of the sorting Collections)
    is most likely the better solution.
    Andreas Leitgeb, Oct 27, 2008
    #3
  4. justineee

    Roedy Green Guest

    On Sun, 26 Oct 2008 05:48:10 -0700 (PDT), justineee
    <> wrote, quoted or indirectly quoted someone
    who said :

    >I am making a project that runs like a word storage. The user inputs
    >words and stores it in arrays. Also, the user can delete words and
    >display the new collection of words. However, I am having problems in
    >deleting an element in an array.


    The easy way is to use an ArrayList, and let remove figure out how to
    do the sliding.

    Strictly speaking you can't delete an element. To have a smaller
    array you need to allocate a new array.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    The Canadian national animal should be changed from the beaver to the ostrich.
    Canadians elected a party that denies global warming so they too could pretend it presents no danger.
    Roedy Green, Oct 27, 2008
    #4
    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. Harry Barker
    Replies:
    2
    Views:
    507
    Alf P. Steinbach
    Apr 19, 2006
  2. Umesh

    Deleting Array Element

    Umesh, Jun 13, 2007, in forum: C Programming
    Replies:
    13
    Views:
    491
    John Bode
    Jun 14, 2007
  3. HANM
    Replies:
    2
    Views:
    698
    Joseph Kesselman
    Jan 29, 2008
  4. Sean Nakasone
    Replies:
    1
    Views:
    178
  5. crea
    Replies:
    2
    Views:
    399
    Nobody
    Dec 28, 2012
Loading...

Share This Page