如何将Arraylist拆分成更小的列表?

时间:2016-02-13 13:27:25

标签: java arraylist

public Student(String name, String sch, int stay, String year)
{
    String name1 = getInput("Enter your name:");
    this.name = name1;  
    String sch1 = getInput("Enter your scholarship:");
    this.sch = sch1;    
    String number = getInput("Enter years of stay: ");
    this.stay = Integer.parseInt(number);

    for (int i=0; i < programList.size(); i++)
    {
        if (stay==1)
        {
        //If the student's year of stay is only 1 year I will  put he/she to the "freshmenList". How will I do that?
        }

    }

这是我的代码。我想根据停留输入的年数将输入的元素排序到新的ArrayList。 (如果入住年份= 1则为新生arraylist,如果= 2则为二年级学生,等等)。我该怎么做?谢谢!

1 个答案:

答案 0 :(得分:0)

我建议您使用switch,它们的可读性更高(imo),并且应该比一堆if语句更快:

switch (stay)
{
    case 1:
        freshmenList.add(this);
        break;

    case 2:
        sophomoreList.add(this);
        break;

    ...
    default:
        //Do something with invalid answers
}