Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: not able to remove from ArrayList

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default not able to remove from ArrayList

    Hi,
    I am trying to remove an object from a array list by passing in the object to be removed.
    I have a equals method as well. But somehow its not woking. The equals method is not getting picked.
    Please suggest whats wrong

    Heres my code:
    import java.util.*;
    class Dummy
    {
    	int x;
    	Dummy(int y)
    	{
    		x=y;
    	}
    	public boolean equals(Dummy o)
    	{
    		if(o.x==this.x)
    			return true;
    		return false;
    	}
    	public String toString()
    	{
    		return Integer.toString(x);
    	}
    }
    class CollTest {
    public static void main(String args[]) {
     
    ArrayList al = new ArrayList();
    System.out.println("Initial size of al: " +al.size());
     
    // add elements to the array list
    al.add(new Dummy(5));
    al.add(new Dummy(6));
    al.add(new Dummy(7));
     
    System.out.println("Size of al after additions: " +al.size());
    // display the array list
    System.out.println("Contents of al: " + al);
     
    // Remove elements from the array list
    al.remove(new Dummy(5));
    System.out.println("Size of al after deletions: " +al.size());
    System.out.println("Contents of al: " + al);
    }
    }

    Output is:
    Initial size of al: 0
    Size of al after additions: 3
    Contents of al: [5, 6, 7]
    Size of al after deletions: 3
    Contents of al: [5, 6, 7]


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: not able to remove from ArrayList

    The problem is because the equals method must be able to compare the Dummy object to any other object (not necessarily another Dummy object). Implementing it as public boolean equals(Dummy o) only accepts inputs of type Dummy, thus doesn't properly override the equals method.

    One way you can ensure that you're properly overriding a method is to add a @Override annotation before the method declaration, and it will generate a compile error if the method isn't properly overridden.

    @Override
    public boolean equals(Object o)
    {
        if(o instanceof Dummy) // check to make sure o isn't null, and that o is a Dummy object
        {
            // TODO: test for comparing two Dummy objects
        }
        return false; // either o is null or isn't a Dummy object
    }

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    harsha_c (March 3rd, 2011)

  4. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: not able to remove from ArrayList

    Thanks alot..
    did a dumb thing by using Dummy

    public boolean equals(Dummy o)

    Object.equals() was getting picked and not this.

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  2. Help Remove file from pad directory
    By georgybaja in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 8th, 2011, 05:29 PM
  3. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 03:08 PM
  4. How to remove letters
    By noobish in forum Java Theory & Questions
    Replies: 13
    Last Post: October 3rd, 2009, 11:36 PM
  5. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 02:11 PM