-1
我想指的是我在方法中啓動的ArrayList,但是當我嘗試從該ArrayList中刪除某些內容時,出現錯誤,指出我的ArrayList的大小爲0.但是,在設置ArrayList的值之後,它會打印出我稱爲「學校」的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);
}
謝謝,我感謝幫助!
此代碼看起來不像它編譯。這是實際的代碼嗎?如果沒有,請儘自己的本分,通過向社區提供實際的代碼來幫助自己,這樣我們就不會追逐那些並不是真正的bug。 :)例如,你有一個沒有右括號的for循環,並且你已經開始評論一半的代碼而沒有結束評論。這使得很難說出這裏發生了什麼。 – jmort253 2012-04-06 05:52:52