【问题标题】:ArrayIndexOutOfBounds but array size properly setArrayIndexOutOfBounds 但正确设置了数组大小
【发布时间】:2015-03-29 08:28:11
【问题描述】:

我在程序输出过程中遇到了这个错误,它立即结束。总共只显示了 8 个学生对象。下面是显示错误的输出的 sn-p:

********************************************************************
********************************************************************
Student ID: 3
Student Title: Mrs
Student First Name: Sophie
Student Last Name: Chua
Student DOB: 3/3/2003
Assignment 1: 65
Assignment 2: 78
Practical: 7
Exam: 92
Overall: 81.6
Final Grade: HD
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20
********************************************************************
********************************************************************
Student ID: 4
Student Title: Mrs
Student First Name: Brendon
Student Last Name: Low
Student DOB: 4/4/2004
Assignment 1: 95
Assignment 2: 98
Practical: 7
Exam: 69
    at student.StudentClient.main(Overall: 80.1
StudentClient.java:420)
Final Grade: HD
********************************************************************
********************************************************************

测试数组对象的声明:

Student[] studentList = new Student[8];
studentList[0] = new Student();
studentList[1] = new Student();
studentList[2] = new Student();
studentList[3] = new Student();
studentList[4] = new Student();
studentList[5] = new Student();
studentList[6] = new Student();
studentList[7] = new Student();
studentList[8] = new Student();
studentList[9] = new Student();
studentList[0].setStudent("Mr", "Jason", "Lee", 1, 1, 1, 2001, 85, 75, 8, 65, 72.5, "D");
studentList[1].setStudent("Miss", "Candice", "Teo", 2, 2, 02, 2002, 95, 95, 9, 90, 92.0, "HD");
studentList[2].setStudent("Mrs", "Sophie", "Chua", 3, 3, 03, 2003, 65, 78, 7, 92, 81.6, "HD");
studentList[3].setStudent("Mrs", "Brendon", "Low", 4, 4, 04, 2004, 95, 98, 7, 69, 80.1, "HD");
studentList[4].setStudent("Mr", "Clarance", "Yeo", 5, 5, 05, 2005, 80, 76, 5, 59, 65.7, "C");
studentList[5].setStudent("Mr", "Adrian", "Tan", 6, 6, 6, 2006, 70, 60, 4, 20, 40.0, "ND");
studentList[6].setStudent("Ms", "Debbie", "Toh", 7, 7, 7, 2007, 60, 60, 7, 50, 56.0, "P");
studentList[7].setStudent("Miss", "Sarah", "Ho", 8, 8, 8, 2008, 59, 61, 7, 62, 62.0, "N");
studentList[8].setStudent("Mr", "Aloysius", "Lim", 9, 9, 9, 2009, 62, 63, 8, 69, 67.5, "C");
studentList[9].setStudent("Mrs", "Peggy", "Lo", 10, 10, 10, 2010, 65, 67, 9, 72, 71.4, "D");

冒泡排序方法:

public static void BubbleSort(Student[] st) {

Student temp;   //holding variable
boolean changed = false;
for (int j = 0; j < st.length - 1; j++) {
    if (st[j] != null) {
        long studentID1 = st[j].getStudentID();
        if (st[j + 1] != null) {
            long studentID2 = st[j + 1].getStudentID();
            if ((st[j] != null) && (st[j + 1] != null)) {
                if (studentID1 > studentID2) // change to > for ascending sort
                {
                    temp = st[j];  //swap elements
                    st[j] = st[j + 1];
                    st[j + 1] = temp;  //shows a swap occurred  
                    changed = true;
                }
            }
        }
    }
}
if (changed) {
    BubbleSort(st);
}
}

主要方法:

BubbleSort(studentList);
    for (int i = 0; i <= studentList.length; i++) {
        if (studentList[i] != null) {
            studentList[i].writeOutput();
        }
}

我以前没有遇到过这个错误,我不确定我的代码的哪一部分导致了这个错误。当我通过学生 ID 运行学生对象的 BubbleSort 并显示它们时,就会显示错误。

如果需要我的代码的任何其他部分来解决此问题,请告诉我。谢谢

【问题讨论】:

  • “我不确定我的代码的哪一部分导致了这个错误”——这就是异常中的堆栈跟踪的用途。这显示了哪条线路导致了问题。你还应该学会使用调试器,这样你就可以通过单步调试代码来诊断这类事情。

标签: java arrays netbeans indexoutofboundsexception bubble-sort


【解决方案1】:

数组的长度与您尝试放入其中的元素数量不匹配。

Student[] studentList = new Student[8]; // change to 10
studentList[0] = new Student();
studentList[1] = new Student();
studentList[2] = new Student();
studentList[3] = new Student();
studentList[4] = new Student();
studentList[5] = new Student();
studentList[6] = new Student();
studentList[7] = new Student();
studentList[8] = new Student(); // 8 is an invalid index for an array of length 8
studentList[9] = new Student(); // 9 is an invalid index for an array of length 8

也改变

for (int i = 0; i <= studentList.length; i++)

for (int i = 0; i < studentList.length; i++)

【讨论】:

  • 感谢@Aran 的建议。我已将其更改为 10。现在显示所有学生对象。但是,错误仍然显示在输出的相同位置,在学生对象 3 和 4 的输出之间。
  • 谢谢@Eran!进行建议的更改后,错误已解决。
猜你喜欢
  • 2014-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-17
  • 2018-12-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多