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
}
}
ArrayList.What do you know about comparing twoStringobjects?ArrayList, see what the opposite ofaddmight be and give it a try. If it doesn't work, show what you tried and we can help.