无法使arraylist使用索引和while循环创建列表

时间:2017-09-22 13:39:04

标签: arraylist

我正在尝试在java中创建一个程序,您可以添加人们的生日,姓名,出生月份和分娩年份。我希望能够打印出一个月份和每个月出生人数的列表。我有以下代码,其中2个类作为“人”类和“分析器”类。以下是代码

import java.util.*;
/*
 * This project will keep track of which day
 * (1 to 31) and which month (1 to 12)in which
 * people are born.
 * Look to LogAnalyzer for clue.
 */

public class Person
{
private int birthDay;
private int birthMonth;
private int birthYear;
private String name;

 public Person(String name, int birthDay, int birthMonth, int birthYear)
{ 
    this.name = name;
    if(birthDay >= 1 && birthDay <= 31){
        this.birthDay = birthDay;
    }
    else {
        this.birthDay = -1;
    }

    if(birthMonth >= 1 && birthMonth <= 12 ){
        this.birthMonth = birthMonth;
    }
    else {
        this.birthMonth = -1;
    }

    this.birthYear = birthYear;

}

 public String getName()
{ 
    return name;
}

public int getBirthDay()
{
    return birthDay;
}

  public int getBirthMonth()
{
    return birthMonth;
}

public int getBirthYear()
{
    return birthYear;
}

 public String toString()
{
    return "Name: " + getName() + "    BirthDay: " + getBirthDay() + 
    "    BirthMonth: " + getBirthMonth() + "    BirthYear: " + 
getBirthYear();
}


}

import java.util.ArrayList;
import java.util.Iterator;

public class Analyzer
{
// instance variables - replace the example below with your own
private int []birthDayStats;
private int []birthMonthStats;
private ArrayList people;

/**
 * Constructor for objects of class Analyzer
 */
public Analyzer()
{
   people = new ArrayList();
}

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]++;
        birthDayStats[birthDay]++;
    }
    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 (birthMonthStats[index]);
        index++;
    }
}

}

我遇到问题的代码是“printmonthlist”方法,我正在尝试打印它但不打印。我希望它打印出12个月的月份清单和每个月出生的人数。如果你们中的任何一个人能帮助我解决这个问题。

1 个答案:

答案 0 :(得分:0)

您忘记初始化阵列了。正如您在此处的代码中所看到的,我在Analyzer创建中使用两个常量初始化了数组。 注意:为了便于阅读,我只是更改了printMonthList()方法

中的日志
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>();
       // A default value of 0 for arrays of integral types is guaranteed by the language spec
       this.birthDayStats = new int[Analyzer.DAYS_PER_MONTH];
       // A default value of 0 for arrays of integral types is guaranteed by the language spec
       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++;
        }
    }
}