Results 1 to 12 of 12
  1. #1
    moamen is offline Member
    Join Date
    Sep 2009
    Posts
    15
    Rep Power
    0

    Default deleting an element from an array

    i have an array(phone[]) that hold some elemnts of type Contacts..
    i want to search for a contact and delete it.

    i search this contact by his name and put all simmilar contacts in a new array
    refering to the phone [] ..

    now the problem is how to choose a contact from the new array and delete him in the phone[]......

    i thought of taking the refrence of the chosen contact from the new array and send it to the phone[] but i don't know how to get the corresponding index in the phone[] for this refrence....

    plz help me!!!!
    thanx

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  3. #3
    moamen is offline Member
    Join Date
    Sep 2009
    Posts
    15
    Rep Power
    0

    Default

    HashMap?????????!!!!!!!

    i don't know hashmap it is required from me to be in array!!!!

    thx for your reply!!

    and if there any way to get the index corresponding to a refrence please tell me!!

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,787
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by moamen View Post
    HashMap?????????!!!!!!!

    i don't know hashmap it is required from me to be in array!!!!

    thx for your reply!!

    and if there any way to get the index corresponding to a refrence please tell me!!
    Why did you put in so many exclamation marks and question marks in your reply? There is no need to shout. btw, a Map is a very sensible suggestion. If you have bounding restrictions you should've told us beforehand.

    kind regards,

    Jos

  5. #5
    moamen is offline Member
    Join Date
    Sep 2009
    Posts
    15
    Rep Power
    0

    Default

    i really didn't mean to shout!
    i was just kiddin' i'm sorry if you thought i was shouting!

    for hashMap i don't know it and the assignment requires to be an array!

    and please if there other way plz tell me!

    moamen!

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,787
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by moamen View Post
    i really didn't mean to shout!
    i was just kiddin' i'm sorry if you thought i was shouting!

    for hashMap i don't know it and the assignment requires to be an array!

    and please if there other way plz tell me!
    The following little method deletes the element at index position 'd'; it effectively creates a new array and returns it:

    Java Code:
    String[] delete(String[] array, int d) {
       String[] narray= new String[array.length-1]; // one element less
       for (int i= 0, j= 0; i < array.length; i++)
          if (i != d) // copy if not element d
             narray[j++]= array[i];
       return narray; // return new array
    }
    That should do the job.

    kind regards,

    Jos

  7. #7
    moamen is offline Member
    Join Date
    Sep 2009
    Posts
    15
    Rep Power
    0

    Default

    Thank you !!

    now i have another questiion !!:S

    i want to get the index in an array that correspnds it's refrence
    e.g
    i have a contact which refrenc is let's say Contact@c125 how can i get it's index!?!

    thx in advance
    moamen

  8. #8
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    6

    Default

    for(int i=0; array[i]!="Contact@c125"; i++);
    I think this will work, haven't tested it with objects, but if it was a string array it would work. Worth a try.

  9. #9
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    for(int i=0; !array[i].equals("Contact@c125"); i++);

    would be better

  10. #10
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    6

    Default

    I dont think so because it is an array of object, and Contact@c125 is an object ?adress?. You want to use == to check if the references are the same. Not positive though. Maybe someone else can clarify this.
    Last edited by collin389; 01-02-2010 at 05:16 AM.

  11. #11
    moamen is offline Member
    Join Date
    Sep 2009
    Posts
    15
    Rep Power
    0

    Default

    Thanx for the replys !!!
    but i noticed somthing very wierd in my project!!:)
    i delete a contact by searching by his name and putting all the similars in an array (fConts[] and make it refere to the main array phone[]) then i choose the one i want by entreing his number in the new array to get its refrence in the new array which is his refrence in the phone[] and compare refrences int the phone[] till i get a match and send its index in the phone[] to a delete method!! and this works but when i wanted to check it by myself and printed all refrences by using the ways you (collin and lil Aziz btw thank you ) i noticed that no refrences are matching but it works.:S:S how?!!)

    thanx
    moamen

  12. #12
    raqman is offline Member
    Join Date
    Sep 2009
    Posts
    37
    Rep Power
    0

    Default

    You could use commons lang's ArrayUtils.

    array = ArrayUtils.removeElement(array, element)
    RAQ Report: free Java reporting tool.

Similar Threads

  1. Deleting from an array
    By Dieter in forum Advanced Java
    Replies: 13
    Last Post: 09-25-2009, 10:27 AM
  2. [SOLVED] indexing an element in an array help!
    By anthonym2121 in forum New To Java
    Replies: 1
    Last Post: 04-03-2009, 07:21 PM
  3. Replies: 1
    Last Post: 03-31-2009, 07:40 AM
  4. Unique element in an array
    By revathi17 in forum New To Java
    Replies: 2
    Last Post: 12-31-2007, 09:44 AM
  5. Max element in an Array
    By mew in forum New To Java
    Replies: 5
    Last Post: 12-03-2007, 06:26 PM

Posting Permissions

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