[英]How to loop to find maximum value in a Arraylist
I have this project that i'm making that you import your birthday, name, birthyear, and birthmonth. 我正在做的这个项目是您输入生日,姓名,出生年月和出生月。 I'm trying to make a method that finds the most popular birthday for example, "the most popular date is the 16th with blank birthdays. I know I would have to loop around birthDayStats but how would I write that? here's my code, and the method i'm having trouble with is on the bottom. 我正在尝试创建一种方法来查找最受欢迎的生日,例如,“最受欢迎的日期是16岁,生日空白。我知道我必须循环使用birthDayStats,但是我该怎么写呢?这是我的代码,我遇到麻烦的方法是在底部。
import java.util.ArrayList;
import java.util.Collections;
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 void mostPopularDay() { //finds the most popular Day of the year
Object obj = Collections.mostPopularDay(birthDayStats);
System.out.println(obj);
}
}
You have to iterate over your array which holds the days, find the highest count and return the index of that value. 您必须遍历保存日期的数组,找到最高计数并返回该值的索引。
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;
}
}
return popularDay + 1; //Adding +1 since there is no 0th day of the month
}
Keep in mind, that this does only return the most popular day for birthdays, not the most popular date. 请记住,这只会返回生日中最受欢迎的日期,而不是最受欢迎的日期。 But I assume that this is what you wanted. 但是我认为这就是您想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.