繁体   English   中英

如何从数组列表中返回一个对象

[英]how to return an object from an arraylist

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

  • 姓名
  • 出生月
  • 出生年
  • 生日

它们都存储在一个数组列表中。 如果我创建一个调用的方法,是否可以通过调用月份来返回某些对象?

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;
}

你可以这样做:

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 ;

    }
});
}

这是我相信您正在寻找的内容:

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM