Results 1 to 3 of 3
  1. #1
    jmohandos304 is offline Member
    Join Date
    Apr 2014
    Posts
    90
    Rep Power
    0

    Default Removing elements from ArrayList

    Referring Code 1, the book says line 16 of the code removes the element "Three" but line 17 does not remove the element "Four" because of Statement 1. The question is does remove(Object o) method invoke the == or the equals method because statement 1 and 2 seem to be in conflict

    Statement 1:
    Two objects are equal if their object references point to the same object. (which is nothing but definition of ==)

    Statement 2:
    The author refers to Statement 1 and says "As mentioned earlier, the method remove compares the objects for equality before removing it from ArrayList by calling method equals."

    Code 1:
    Java Code:
    import java.util.ArrayList;
    public class DeleteElementsFromArrayList {
     public static void main(String args[]) {
     ArrayList<StringBuilder> myArrList = new ArrayList<>();
     StringBuilder sb1 = new StringBuilder("One");
     StringBuilder sb2 = new StringBuilder("Two");
     StringBuilder sb3 = new StringBuilder("Three");
     StringBuilder sb4 = new StringBuilder("Four");
     myArrList.add(sb1);
     myArrList.add(sb2);
     myArrList.add(sb3);
     myArrList.add(sb4);
     myArrList.remove(1); 
     for (StringBuilder element:myArrList) 
     System.out.println(element); 
     myArrList.remove(sb3); 
     myArrList.remove(new StringBuilder("Four")); 
     System.out.println();
     for (StringBuilder element : myArrList) 
     System.out.println(element); 
     }
    }

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

    Default Re: Removing elements from ArrayList

    The StringBuilder class doesn't override the equals() method, nor the hashCode() method, so the implementations for the Object class are inherited. Its equals() method simply does 'this == that'.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    448
    Rep Power
    6

    Default Re: Removing elements from ArrayList

    Whenever an object is removed from arraylist, the respective object's equals methods will be called to make sure the object which passes in remove method is already available in list.

    However, according to StringBuilder class, it does not override equal method. It does mean that it will have typical Object class equal method by default.

    As Object's equal method just checks object reference but not object value, line 17 does not remove the object. Instead of StringBuilder, if you use String, it will work. Because String has already its own equals method.
    Mak
    (Living @ Virtual World)

Similar Threads

  1. Adding and Removing elements from arrays
    By NoobieCode in forum New To Java
    Replies: 7
    Last Post: 04-25-2013, 09:47 AM
  2. Removing spaces in String ArrayList
    By dougie1809 in forum New To Java
    Replies: 6
    Last Post: 03-13-2012, 09:25 PM
  3. [HELP] Removing objects from an ArrayList!
    By bpx95 in forum New To Java
    Replies: 4
    Last Post: 04-30-2011, 05:54 AM
  4. Removing from an ArrayList while looping thru
    By pahiker in forum New To Java
    Replies: 7
    Last Post: 06-22-2010, 07:38 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
  •