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
,它们的可读性更好(我认为),并且应该比一堆 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/