Java 将ArrayList转换为2D数组-返回ArrayOutOfBounds

Java 将ArrayList转换为2D数组-返回ArrayOutOfBounds,java,arrays,arraylist,indexoutofboundsexception,indexoutofrangeexception,Java,Arrays,Arraylist,Indexoutofboundsexception,Indexoutofrangeexception,我需要创建一个学生列表,并将其转换为座位表(特定长度和宽度的2d数组)。这是我的密码: public class SeatingChart { Student seats[][]; public SeatingChart(List <Student> studentList, int rows, int cols) { seats = new Student[rows][cols]; for (int i = 0; i < cols; i++) { //

我需要创建一个学生列表,并将其转换为座位表(特定长度和宽度的2d数组)。这是我的密码:

public class SeatingChart {
  Student seats[][];
  public SeatingChart(List <Student> studentList, int rows, int cols) {
    seats = new Student[rows][cols];
    for (int i = 0; i < cols; i++) { //Assigns Students in Column by Row order
        for (int j = 0; j < rows; j++) {
            if (i*rows+j > studentList.size() - 1) {
                seats[i][j] = null; //Occupies remaining `seats` with null
            }
            seats[i][j] = studentList.get(i*rows+j); //Finds an error with this line
        }
    }
}

public class Tester {
  public static void main (String[] args) {
    List <Student> classroom = new ArrayList<Student>();
    classroom.add(new Student("Anna", 3));
    SeatingChart sc = new SeatingChart(classroom, 2, 2); //Finds an error with this line
  }
}

public class Student {
  //assigns Name and number of absences for each student
}
公共类座位表{
学生座位[];
公共座位表(列出学生名单、整数行、整数列){
座位=新生[排][列];
对于(int i=0;istudentList.size()-1){
seats[i][j]=null;//用null占据剩余的'seats'
}
seats[i][j]=studentList.get(i*rows+j);//查找此行的错误
}
}
}
公共类测试员{
公共静态void main(字符串[]args){
列表教室=新建ArrayList();
增加(新生(“安娜”,3));
SeatingChart sc=新的座位图(教室,2,2);//发现此行有错误
}
}
公立班学生{
//指定每个学生的姓名和缺勤次数
}
这将返回:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    at java.util.ArrayList.rangeCheck(ArrayList.java:657)
    at java.util.ArrayList.get(ArrayList.java:433)
    at stud2.SeatingChart.<init>(SeatingChart.java:14)
    at stud2.SeatingChartTester.main(SeatingChartTester.java:12)
线程“main”java.lang.IndexOutOfBoundsException中的异常:索引:1,大小:1 位于java.util.ArrayList.rangeCheck(ArrayList.java:657) 获取(ArrayList.java:433) 在stud2.SeatingChart.(SeatingChart.java:14) 在stud2.SeatingChartTester.main(SeatingChartTester.java:12)
但是,作为ArrayList类(rangeCheck和get)一部分的错误当我在
教室中再增加两个学生时,错误就消失了。为什么错误被解决了,为什么最后两个错误仍然存在?我已经仔细检查了代码背后的逻辑,并用与ArrayList大小相同的1D数组替换了2D数组,但我得到了相同的错误。

在这里我可以看到问题

  List <Student> classroom = new ArrayList<Student>();
classroom.add(new Student("Anna", 3)); //so size of class room is 1
现在注意

 for (int i = 0; i < cols; i++) { 
    for (int j = 0; j < rows; j++) { // the first iteration in this loop will go fine
        if (i*rows+j > studentList.size() - 1) {
            seats[i][j] = null;
        }
        seats[i][j] = studentList.get(i*rows+j);
 /* but when j is one and i is 0 this expression resolves to i*rows + j == 0*2 + 1 = 1
   means you're saying studentList.get(1) , but list has only one student which is at 
   index 0 , that  
   is why you're getting java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 cuz 
   index 
   is 1 in the get(1) and size is also 1 means noting at index 1.*/
    }
}
for(inti=0;istudentList.size()-1){
席位[i][j]=零;
}
seats[i][j]=studentList.get(i*rows+j);
/*但是当j为1,i为0时,这个表达式解析为i*rows+j==0*2+1=1
意思是你们说的是studentList.get(1),但列表中只有一个学生在
索引0,即
这就是您获取java.lang.IndexOutOfBoundsException的原因:索引:1,大小:1 cuz
指数
get(1)中的值为1,size也为1表示在索引1处注释*/
}
}

@maio290我觉得我用SeatingChart中的if语句检查了ArrayList上存在越界的情况。我发现您需要的只是一个else语句。但是,它现在导致了一个nullPointerException,我找到了它的原因。谢谢!
 for (int i = 0; i < cols; i++) { 
    for (int j = 0; j < rows; j++) { // the first iteration in this loop will go fine
        if (i*rows+j > studentList.size() - 1) {
            seats[i][j] = null;
        }
        seats[i][j] = studentList.get(i*rows+j);
 /* but when j is one and i is 0 this expression resolves to i*rows + j == 0*2 + 1 = 1
   means you're saying studentList.get(1) , but list has only one student which is at 
   index 0 , that  
   is why you're getting java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 cuz 
   index 
   is 1 in the get(1) and size is also 1 means noting at index 1.*/
    }
}