Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ArrayList范围检查错误_Java_Arraylist - Fatal编程技术网

Java ArrayList范围检查错误

Java ArrayList范围检查错误,java,arraylist,Java,Arraylist,我试图引用我在方法中启动的arraylist,但是当我试图从该arraylist中删除某些内容时,我得到一个错误,即我的arraylist的大小为0。但是,在我设置ArrayList的值之后,它会打印出ArrayList的大小,我称之为“school”。这是我的密码 import java.util.*; public class SchoolSolver{ public static ArrayList<Girl> school = new ArrayList<Gir

我试图引用我在方法中启动的arraylist,但是当我试图从该arraylist中删除某些内容时,我得到一个错误,即我的arraylist的大小为0。但是,在我设置ArrayList的值之后,它会打印出ArrayList的大小,我称之为“school”。这是我的密码

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);
}
import java.util.*;
公立学校{
公共静态ArrayList学校=新ArrayList();
使用的公共静态ArrayList=新ArrayList();
公共静态void main(字符串[]args){
重置学校();
//System.out.println(school.size());
布尔不可能=假;
对于(int i=0;i<7;i++){
如果(不可能){
重置学校();
}否则{
newDay();
}
对于(int j=0;j<5;i++){
//randomGirl()是我出错的地方
女孩领袖=女孩();
使用。添加(引线);
}
}
}
/*下面是我怀疑错误可能来自的两种方法*/
公立学校(){
学校。清除();
对于(int i=0;i<15;i++){
//System.out.println(“ran”);
学校。添加(新女孩(i));
}
(女孩g:学校){
ArrayList=新建ArrayList();
对于(int i=0;i<15;i++){
如果(i!=g.getID()){
同学们。添加(新女孩(我));
}
}
g、 同学(同学),;
}
}
公共静态女孩{
返回学校。删除(0);
}

谢谢你,我很感谢你的帮助!

你有一个永无止境的循环

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

查看如何增加
i
而不是
j
。这意味着您继续调用
randomGirl
,这将在每次调用时从列表中删除
Girl
。因此,过了一段时间(15次)您的列表变为空,您仍然调用
randomGirl
,这会导致异常

此代码看起来不像是编译的。这是实际的代码吗?如果不是,请尽自己的一份力量,向社区提供您的实际代码,这样我们就不会追踪您真正遇到的错误。:)例如,您在没有右括号的情况下创建for循环,并且在没有右括号的情况下开始注释一半的代码。这使得很难判断这里发生了什么。