[英]How to split Arraylist into smaller lists?
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?
}
}
Here's my code. 这是我的代码。 I want to sort the inputed elements to a new ArrayList according to the years of stay input.
我想根据停留输入的年份将输入的元素排序到新的ArrayList中。 (If year of stay = 1 then freshmen arraylist, if=2 then sophomore arraylist, so on).
(如果居住年限= 1,则为新生大名单,如果= 2,则为二年级大名单,依此类推)。 How will I do that?
我该怎么做? Thank you!
谢谢!
I recommend that you would use a switch
for this, they are a bit better readable (imo) and are supposed to be faster than a bunch of if
statements: 我建议您为此使用一个
switch
,它们的可读性更好(imo),并且应该比一堆if
语句更快:
switch (stay)
{
case 1:
freshmenList.add(this);
break;
case 2:
sophomoreList.add(this);
break;
...
default:
//Do something with invalid answers
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.