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,则为新生大名单,如果= 2,则为二年级大名单,依此类推)。我该怎么做?谢谢!

最佳答案

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

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

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

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

关于java - 如何将Arraylist拆分为较小的列表?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35380497/

10-10 08:41