Results 1 to 4 of 4
  1. #1
    hamed is offline Member
    Join Date
    Sep 2010
    Posts
    11
    Rep Power
    0

    Default How do I remove objects from an Array in java?

    Hi. I really appreciate it if some one help me with this.
    let's say foo is an Array of Strings, and it has the following values:
    foo[0]="a";
    foo[1]="cc";
    foo[2]="a";
    foo[3]="dd";
    1)What do I have to do to delete/remove all the Strings/Objects equal to "a" in the Array?
    2) What do I have to do to delete/remove all the Strings and make my array free of any value?

    Thanx!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    1)What do I have to do to delete/remove all the Strings/Objects equal to "a" in the Array?
    Use a loop to search thru the array for the element.
    Then it depends what remove means: Replace its value with a null, or move all the following elements down over the top of the one to replace, leaving the null element at the end of the array.

    To empty it, fastest would be do define a completely new array.

  3. #3
    hamed is offline Member
    Join Date
    Sep 2010
    Posts
    11
    Rep Power
    0

    Default

    Thanx for ur quick answer. I'm writing a method which should delete some matching values from the array event from a calendar. Do u think if this method will work?(appreciate it).

    public void removeEvent(Event event){
    boolean [] deleteItem = new boolean[index];
    for(index=0;index<10;index++){
    if(events[index].equals(events)){
    deleteItem[index]=true;
    events[index]="null";
    numEvents--;
    }

    else{
    deleteItem[index]=false;
    }
    }
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Do u think if this method will work?
    Best way to get an answer is to compile and execute the code and see what happens.
    I don't know what you intend to do in the code you posted. Could you add comments saying what each statement is supposed to do. For example what is this statement supposed to do:
    Java Code:
    if(events[index].equals(events)){
    Please use code tags when posting code: Java Forums - BB Code List

Similar Threads

  1. Replies: 5
    Last Post: 02-25-2009, 07:14 PM
  2. how to remove an object from the array list
    By cecily in forum New To Java
    Replies: 3
    Last Post: 08-02-2007, 02:26 PM
  3. Replies: 1
    Last Post: 08-01-2007, 08:55 AM
  4. Array with objects
    By toby in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 09:50 AM
  5. array of objects
    By Jack in forum New To Java
    Replies: 2
    Last Post: 07-02-2007, 05:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •