2

I have written my own class called Person with the variables String name, int age and double height, as well as a toString(). I have then created an ArrayList of Persons, and added a couple of instances. It prints well. Now I want to write a method that, when I write a name, checks the ArrayList for instances with that name, an if so, removes that instance. How should I do this?

Here is what I've written:

import java.util.*;
public class PersonManager {

    public static void main(String[] args) {
        ArrayList<Person> people = new ArrayList<>();
        Scanner keyboard = new Scanner(System.in);

        people.add(new Person("Adam ", 29, 177.5));
        people.add(new Person("Bernadette", 19, 155.2));
        people.add(new Person("Carl", 45, 199));

        for (Person p : people)
            System.out.println(p);

        System.out.println("Select person to remove");
        String name = keyboard.nextLine();


        // if there is a person with that name in the list, that
        //person gets removed from the list

    }

}
3
  • 4
    How do you think you should do it? Commented May 20, 2015 at 20:10
  • Looks like you already know how to loop through an ArrayList. What do you know about comparing two String objects? Commented May 20, 2015 at 20:12
  • Take a look at the methods of ArrayList, see what the opposite of add might be and give it a try. If it doesn't work, show what you tried and we can help. Commented May 20, 2015 at 20:14

3 Answers 3

1

If you are using JAVA 8 and don't mind of creating a new List from the original you can use JAVA 8 stream like that :

    ArrayList<Person> people = new ArrayList<>();

    people.add(new Person("Adam ", 29, 177.5));
    people.add(new Person("Bernadette", 19, 155.2));
    people.add(new Person("Carl", 45, 199));
    String nameToRemove = "name";
    people = people.stream().filter((t) -> !t.getName().equals(nameToRemove)).collect(Collectors.toList());
Sign up to request clarification or add additional context in comments.

Comments

1

As @MasterMind said: if you have access to JDK 8 features, you can either use filtering (as shown in his example) or alternatively, use the new Collection#removeIf(..) method. In your case, that would be something like:

people.removeIf(person -> person.getName().equals(name));

See here for a fully working example.

Comments

0

You want to navigate to Person with given name and remove it from the list.
So you can use iterator while going through list:

Iterator personIter = people.iterator();
while(personIter.hasNext()){
    Person p = (Person)personIter.next();    
    if(name != null && name.equals(p.getName())){
        personIter.remove();
        break; //will prevent unnecessary iterations after match has been found
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.