我正在尝试引用我在方法中启动的数组列表,但是当我尝试从该 ArrayList 中删除某些内容时,我收到一条错误消息,指出我的 ArrayList 的大小为 0。但是,在我设置了ArrayList,它打印出 ArrayList 的大小,我称之为“学校”。这是我的代码。
import java.util.*;
public class SchoolSolver{
public static ArrayList<Girl> school = new ArrayList<Girl>();
public static ArrayList<Girl> used = new ArrayList<Girl>();
public static void main(String[] args){
resetSchool();
//System.out.println(school.size());
boolean notPossible = false;
for(int i = 0; i < 7; i++){
if(notPossible){
resetSchool();
} else {
newDay();
}
for(int j = 0; j < 5; i++){
//randomGirl() is where my error happens
Girl leader = randomGirl();
used.add(leader);
}
}
}
/*Below are the two methods where I suspect the error may originate from */
public static void resetSchool(){
school.clear();
for(int i = 0; i < 15; i++){
//System.out.println("ran");
school.add(new Girl(i));
}
for(Girl g : school){
ArrayList<Girl> classmates = new ArrayList<Girl>();
for(int i = 0; i < 15; i++){
if(i!= g.getID()){
classmates.add(new Girl(i));
}
}
g.setClassmates(classmates);
}
}
public static Girl randomGirl(){
return school.remove(0);
}
谢谢,我很感激帮助!