Stevo4586
Stevo4586

Reputation: 9

How to write a remove method to get rid of object from arraylist

I'm trying to make a program in java that you can add people's birthdays, names, birthmonths, and birthyears. I'm having a hard time trying to come up with the code to remove an object from the arraylist here is the following code. How would I go about writing the removePerson method?

import java.util.ArrayList;

public class Analyzer {
    // instance variables - replace the example below with your own
    private final static int DAYS_PER_MONTH = 31;
    private final static int MONTHS_PER_YEAR = 12;
    private int[] birthDayStats;
    private int[] birthMonthStats;
    private ArrayList<Person> people;

    /**
     * Constructor for objects of class Analyzer
     */
    public Analyzer() {
        this.people = new ArrayList<Person>();
        this.birthDayStats = new int[Analyzer.DAYS_PER_MONTH];
        this.birthMonthStats = new int[Analyzer.MONTHS_PER_YEAR];
    }

    public void addPerson(String name, int birthDay, int birthMonth, int
            birthYear) {
        Person person = new Person(name, birthDay, birthMonth, birthYear);
        if (person.getBirthDay() != -1 || person.getBirthMonth() != -1) {
            people.add(person);
            birthMonthStats[birthMonth - 1]++;
            birthDayStats[birthDay - 1]++;
        } else {
            System.out.println("Your current Birthday is " + birthDay + " or "
                    + birthMonth + " which is not a correct number 1-31 or 1-12 please " +
                    "put in a correct number ");
        }
    }

    public void printPeople() { //prints all people in form: “  Name: Tom   Month: 5   Day: 2  Year: 1965”
        int index = 0;
        while (index < people.size()) {
            Person person = (Person) people.get(index);
            System.out.println(person);
            index++;
        }
    }

    public void printMonthList() { //prints the number of people born in each month
        // Sample output to the right with days being similar
        int index = 0;
        while (index < birthMonthStats.length) {
            System.out.println("Month number " + (index + 1) + " has " +
                    birthMonthStats[index] + " people");
            index++;
        }
    }

    public Person removePerson(String name) {// removes the person from the arrayList
    }
}

Upvotes: 1

Views: 750

Answers (2)

nagendra547
nagendra547

Reputation: 6310

If you are using Java 1.8. It's pretty simple way to do. This will remove Person having 'name' from your list.

 people.removeIf(x -> name.equalsIgnoreCase(x.getName()));

Upvotes: 1

fantaghirocco
fantaghirocco

Reputation: 4878

/**
 * Removes the {@code Person} with the given {@code name} from the list
 * @param name the {@code Person}'s name
 * @return the {@code Person} removed from the list or {@code null} if not found
 */
public Person removePerson(String name) {
    if (name != null) {
        for (Iterator<Person> iter = people.iterator(); iter.hasNext(); ) {
            Person person = iter.next();
            if (name.equalsIgnoreCase(person.getName())) {
                iter.remove();
                return person;
            }
        }
    }
    return null;
}

See the java.util.Iterator#remove() method.


Tuesday learning bonus:

If you want to look for a name faster in your list, you should consider to use a java.util.Map implementation:

HashMap<String,Person> people;

You can add Person objects in a smart way in order to make your search case insensitive:

people.put(name.toLowerCase(), new Person(name, ...));

... and your removePerson method become:

public Person removePerson(String name) {
    if (name != null)
        name = name.toLowerCase();
    return people.remove(name);
}

See the java.util.Map#remove() method.

Upvotes: 1

Related Questions