簡體   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