-2

我正在尝试引用我在方法中启动的数组列表,但是当我尝试从该 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);
}

谢谢,我很感激帮助!

4

1 回答 1

2

你有一个永无止境的循环

for(int j = 0; j < 5; i++)

看看你如何增加i而不是j. 这意味着您继续调用,这会在每次调用时从列表randomGirl中删除 a 。Girl所以过了一会儿(15次)你的列表变空了,你仍然打电话randomGirl,这会导致异常

于 2012-04-06T06:29:27.980 回答