2017-09-26 17 views
1

我想在java中製作一個程序,您可以添加人的生日,名字,birthmonths和birthyears。我很難想出代碼從arraylist中刪除一個對象,這裏是下面的代碼。我將如何去編寫removePerson方法?如何編寫一個刪除方法來擺脫arraylist中的對象

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 
    } 
} 
+0

和人類?那是否覆蓋了那個方法? –

+0

1.您使用迭代器遍歷列表2.您比較您作爲參數傳遞的名稱是否等於當前迭代3的人名。您使用迭代器刪除該人員。 < - 這應該給你足夠的提示,而無需將你的作業解答放在盤子上(儘管我敢打賭有人會盡快做到這一點) –

+0

大聲笑確定會嘗試那些! – Stevo4586

回答

1
/** 
* 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; 
} 

參見java.util.Iterator#remove()方法。


週二學習獎金

如果你想尋找一個名字在名單更快,你應該考慮使用java.util.Map實現:

HashMap<String,Person> people; 

您可以添加Person以明智的方式對象以便搜索不區分大小寫

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

...和你removePerson方法變成:

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

java.util.Map#remove()方法。

+0

與@OH GOD SPIDERS的註釋相同 – fantaghirocco

+0

有人明白我只是在學習java,並且需要字面上的幫助。 – Stevo4586

+1

大家還在學:D – fantaghirocco

1

如果您使用的是Java 1.8。這是非常簡單的方法。這將從您的列表中刪除具有「姓名」的人。

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

upvoted:我忘了! – fantaghirocco

相關問題