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 10 of 10

Thread: ArrayList.remove() problem. (solved)

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Location
    Finland
    Posts
    11
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ArrayList.remove() problem. (solved)

    Hi.

    I created a simple test list while studying ArrayList. For some reason (some obvious i guess) i cant remove objects from list.
    There is tiny class Person with some values, which are added to arraylist. Everything else works, except when i try to remove first occurance of "Pekka". Nothing happens and .remove returns false.

    import java.util.*;
     
    public class MyArrayList2 {
    	public static class Person {
    		private String name;
    		private int id;
     
    		public Person(String name, int id) {
    			this.name = name;
    			this.id = id;
    		}
     
    		public String getName() {
    			return this.name;
    		}
    		public String toString() {
    			return name;
    		}
    	}
     
     
    	public static void main(String[] args) {
    		// Create a list from class Person
    		ArrayList<Person> henkilot = new ArrayList<Person>();
     
    		// Adding few Persons to list
    		henkilot.add(new Person("Maija", henkilot.size() + 1));
    		henkilot.add(new Person("Pekka", henkilot.size() + 1));
    		henkilot.add(new Person("Marko", henkilot.size() + 1));
    		henkilot.add(new Person("Pekka", henkilot.size() + 1));
    		henkilot.add(new Person("Pekka", henkilot.size() + 1));
     
    		// Trying to remove first occurance with name "Pekka"
    		System.out.println(henkilot.remove("Pekka"));
     
    		// Printing out the list
    		System.out.println(henkilot.toString());
     
    	}
    }

    Output is:
    false
    [Maija, Pekka, Marko, Pekka, Pekka]

    Java is:
    java version "1.7.0_55"
    Java(TM) SE Runtime Environment (build 1.7.0_55-b13)
    Java HotSpot(TM) Server VM (build 24.55-b03, mixed mode)
    Last edited by E.K.Virtanen; May 26th, 2014 at 12:39 PM. Reason: Solved


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: ArrayList.remove() problem.

    Your list contains elements of type "Person" but what you are trying to remove is an element of type "String".
    Since the list does not contain any strings it can not remove them either.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ArrayList.remove() problem.

    The remove() method is looking for a String object: "Pekka" in the ArrayList, but the ArrayList contains Person objects so it doesn't find anything to remove.
    If you want to find a Person object that contains the String: "Pekka" you will need to iterate through the list testing each Person object to see if it contains that String and then remove the object that has the String.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Feb 2013
    Location
    Finland
    Posts
    11
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList.remove() problem.

    Ok. I _thougth_ that toString method at class would do the work.
    I stop guessing a go with iterating Thanks for help.

  5. #5
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: ArrayList.remove() problem. (solved)

    The remove method uses the "equals(Object)" method of your objects. You can overwrite the equals method to check for equality with strings. If you do, you do not need to iterate over the entries and can rely on the list doing the work for you.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ArrayList.remove() problem. (solved)

    overwrite the equals method to check for equality with strings.
    @Cornix Do you have some test code that shows what you are saying?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: ArrayList.remove() problem. (solved)

    I was thinking about something like this:
    	public static class Person {
    		private String name;
    		private int id;
     
    		public Person(String name, int id) {
    			this.name = name;
    			this.id = id;
    		}
     
    		public String getName() {
    			return this.name;
    		}
    		public String toString() {
    			return name;
    		}
    		public boolean equals(Object obj) {
    			if (obj instanceof String) {
    				return obj.equals(toString());
    			}
    			return obj == this;
    		}
    	}
    This should enable the OP to remove persons from the list by calling the remove method with a string as argument as long as the string matches the name of the person.


    Edit: Just checked the documentation for the list interface again, this does not work. The list will use the equals method from the object passed as argument to the remove method and not the implementation of the objects contained in the list. Bummer.
    I guess this is a question of personal preference, but I would have preferred it to use the equals method of the contained objects.
    Last edited by Cornix; May 26th, 2014 at 02:59 PM.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ArrayList.remove() problem. (solved)

    Just checked the documentation for the list interface again, this does not work.
    That should be BOLD so it stands out.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: ArrayList.remove() problem. (solved)

    A few days late, but...

    Edit: Just checked the documentation for the list interface again, this does not work. The list will use the equals method from the object passed as argument to the remove method and not the implementation of the objects contained in the list. Bummer.
    I guess this is a question of personal preference, but I would have preferred it to use the equals method of the contained objects.
    Check the contract for the equals method of the Object class...
    It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.

  10. #10
    Junior Member
    Join Date
    Feb 2013
    Location
    Finland
    Posts
    11
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList.remove() problem. (solved)

    This is not the most elegant way, but gives the idea how i decided to solve it. I loop through arraylist until condition comes true, then remove object from arraylist with correct reference.
    import java.util.*;
     
     
    public class MyArrayList2 {
    	public static class Person {
    		private String name;
    		private int id;
     
    		public Person(String name, int id) {
    			this.name = name;
    			this.id = id;
    		}
     
    		public String getName() {
    			return this.name;
    		}
    		public String toString() {
    			return name;
    		}
    	}
     
     
    	public static void main(String[] args) {
    		// Create a list from class Person
    		ArrayList<Person> henkilot = new ArrayList<Person>();
     
    		// Adding few Persons to list
    		henkilot.add(new Person("Maija", henkilot.size() + 1));
    		henkilot.add(new Person("Pekka", henkilot.size() + 1));
    		henkilot.add(new Person("Marko", henkilot.size() + 1));
    		henkilot.add(new Person("Pekka", henkilot.size() + 1));
    		henkilot.add(new Person("Pekka", henkilot.size() + 1));
     
    		// Printing out the original list
    		System.out.println(henkilot.toString());
     
    		// removing first occurance of pekka
    		for(Person name: henkilot) {
    			if(name.toString().equals("Pekka")) {
    				//System.out.println("Pekka löytys");
    				System.out.println(henkilot.remove(name));
    				break;
    			}
    		}
     
    		// Printing out the modified list
    		System.out.println(henkilot.toString());
    	}
    }

Similar Threads

  1. ArrayList remove() implementation problem?
    By jean28 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 23rd, 2013, 07:47 AM
  2. how to remove items of an arraylist
    By crossit in forum Java Theory & Questions
    Replies: 7
    Last Post: December 18th, 2012, 01:14 PM
  3. problem of remove student from list
    By pig-rabbit in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 29th, 2012, 07:14 AM
  4. [SOLVED] Looping through ArrayList to remove elements
    By itispj in forum Collections and Generics
    Replies: 3
    Last Post: October 14th, 2011, 12:42 AM
  5. not able to remove from ArrayList
    By harsha_c in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 3rd, 2011, 03:28 AM