我正在尝试编写一个随机分组人群的程序。我遇到了迭代器的问题。这是代码:
@SuppressWarnings("deprecation")
public static void results(List<String> nameslist) {
Scanner scan = new Scanner(System.in);
int groups = 0;
int count = nameslist.size();
int done=0;
do{
System.out.println("How many people do you want per group?");
groups = scan.nextInt();
} while(groups == 0);
Iterator itr = nameslist.listIterator();
int peopledone=0;
while(peopledone<count){
int groupsdone = 0;
while (groupsdone <= groups){
groupsdone++;
peopledone = 0;
System.out.println("Group "+groupsdone+":");
while (peopledone <= groups){
try{
Object obj = itr.next();
System.out.println(obj);
peopledone++;
}catch (NoSuchElementException e){
System.out.println("Error");
Thread.currentThread().stop();
}
}
}
}
需要注意的几点:
nameslist 是我为测试目的而放在一起的字母 (af) 列表。通常,它们是班级中的人名。
我试图让它只列出名称,直到它用完为止。
非常感谢!