如何从arraylist返回一个对象

时间:2017-10-10 14:03:54

标签: java arraylist

如果我有一个名为person的对象,他们有:

  • 名称
  • 出生月份
  • 出生年份
  • 生日

他们都存放在一个arraylist中。如果我创建一个名为?

的方法,是否可以通过调出月来返回某些对象
public ArrayList returnPersonsForMonth(int month)//returns an ArrayList Person objects and prints out the person object(s)
{
   ArrayList peopleMonth = new ArrayList ()

   return peopleMonth
}

我在java方面相当新,所以如果我问任何愚蠢的话,请原谅我。 其余代码如下

import java.util.ArrayList;
import java.util.Iterator;
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 int mostPopularDay() //finds the most popular Day of the year
{ 
    int popularDay = 0;
    int max = -1;
    for(int i = 0; i < birthDayStats.length; i++) {
        if(birthDayStats[i] > max) {
            max = birthDayStats[i];
            popularDay = i;            
        }
    }
    System.out.println (" The most Popular Date is " + (popularDay + 1) + " with " + max + " birthdays ");
    return popularDay + 1;  //Adding +1 since there is no 0th day of the month

}

public int mostPopularMonth() //finds the most popular Month of the year
{ 
    int popularMonth = 1;
    int max = -1;
    for(int i = 0; i < birthMonthStats.length; i++) {
        if(birthMonthStats[i] > max) {
            max = birthMonthStats[i];
            popularMonth = i;            
        }
    }
    System.out.println (" The most Popular Month is " + (popularMonth + 1) + " with " + max + " birth Months");
    return popularMonth + 1;  //Adding +1 since there is no 0th day of the month

}

public Person removePerson(String name)  // removes the person from the arrayList 
 {
   if (name != null) {
     for (Iterator<Person> iter = people.iterator(); iter.hasNext(); ) {
         Person person = iter.next();
         if (name.equalsIgnoreCase(person.getName())) {
             people.remove(person);
             birthDayStats[person.getBirthDay()-1]--;
             birthMonthStats[person.getBirthMonth()-1]--; 
             return person;
         }
     }
   }
   return null;
}

2 个答案:

答案 0 :(得分:0)

你可以这样做:

public List<Personne> returnPersonsForMonth(int month , List<Personne> personnes )  {

return  CollectionUtils.select(personnes, new Predicate<Personne>() {
    @Override
    public boolean evaluate(Personne p) {
        return p.getMonth() == month ;

    }
});
}

答案 1 :(得分:0)

以下是我认为您正在寻找的内容:

public List<Person> returnPersonsForMonth(List<Person> people_array) {
    List<Person> born_in_month = new ArrayList<>();
    for (Person person : people_array) {
        if (person.month.equals("January")) {
            born_in_month.add(person);
        }
    }
    return born_in_month;
}