gpt4 book ai didi

java - 相对于构造函数抛出越界运行时错误

转载 作者:行者123 更新时间:2023-12-01 18:32:03 25 4
gpt4 key购买 nike

我立即收到源 self 的 Student 的运行时错误类构造函数。这是我知道的唯一错误,但是可能还有更多。

错误内容如下:

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

at java.util.ArrayList.rangeCheck(ArrayList.java:635)

at java.util.ArrayList.set(ArrayList.java:426)

at Student.(Student.java:11)

at TestStudent.main(TestStudent.java:5)

显示我的 IDE 中错误的图形:

run-time error

这是更新后的功能代码:

import java.util.*;
public class Student {
private String name;
private ArrayList<Integer> quizList = new ArrayList<Integer>(5);
public Student(String name, int qz1, int qz2, int qz3, int qz4, int qz5) {
this.name = name;
int qz[] = {qz1, qz2, qz3, qz4, qz5};
for(int i = 0; i < qz.length; i++) {
quizList.add(qz[i]);
}
}

public String getName() {
return name;
}

public String setName(String name) {
return this.name = name;
}

public ArrayList<Integer> getQuizList() {
return quizList;
}

public int getQuiz(int location) {
return quizList.get(location);
}

public void setQuiz(int location, int qz) {
quizList.set(location, qz);
}
}

import java.util.*;
public class TestStudent {
public static void main(String [] args) {
ArrayList<Student> list = new ArrayList<Student>();
list.add(new Student("Mark Kennedy", 70, 80, 90, 100, 90));
list.add(new Student("Max Gerard", 80, 85, 90, 85, 80));
list.add(new Student("Jean Smith", 50, 79, 89, 99, 100));
list.add(new Student("Betty Farm", 85, 80, 85, 88, 89));
list.add(new Student("Dilbert Gamme", 70, 70, 90, 70, 80));

printBook(list);
}

public static void printBook(ArrayList<Student> list) {
for(Student token : list) {
System.out.print(token.getName() + "\t");
for(int i = 0; i < 5; i++) {
System.out.printf("%5d\t", token.getQuiz(i));
}
System.out.println();
}
}

public static void replaceName(ArrayList<Student> list, String find, String replace) {
int position = 0;
int values[] = new int[5];
for(int i = 0; i < list.size(); i++) {
values[i] = list.get(i).getQuiz(i);
}
for(int i = 0; i < list.size(); i++) {
if (list.get(i).getName().equals(find)) {
list.get(i).setName(replace);
}
}
}

public static void replaceQuiz(ArrayList<Student> list, String find, int quizNum, int replace) {
for(int i = 0; i < list.size(); i++) {
if (list.get(i).getName().equals(find)) {
list.get(i).setQuiz((quizNum-1), replace);
}
}
}

public static void replaceStudent(ArrayList<Student> list, String find, Student replacement) {
insertStudent(list, find, replacement.getName());
deleteStudent(list, find);
}

public static void insertStudent(ArrayList<Student> list, String find, String name) {
int position = 0;
int values[] = new int[5];
for(int i = 0; i < list.size(); i++) {
values[i] = list.get(i).getQuiz(i);
}
for(int i = 0; i < list.size(); i++) {
if(list.get(i).getName().equals(find)) {
position = i;
}
}
list.add(position, new Student(name, values[0], values[1], values[2], values[3], values[4]));
}

public static void deleteStudent(ArrayList<Student> list, String find) {
int location = 0;
int i;
for(i = 0; i < list.size(); i++) {
if (list.get(i).getName().equals(find)) {
location = i;
break;
}
}
if (i != list.size()) {
list.remove(location);
}
}
}

更新:

修改了Student中的一些代码:

    private String name;
private ArrayList<Integer> quizList;
public Student(String name, int qz1, int qz2, int qz3, int qz4, int qz5) {
this.name = name;
int qz[] = {qz1, qz2, qz3, qz4, qz5};
for(int i = 0; i < qz.length; i++) {
quizList.add(qz[i]);
}
}

现在抛出此错误:

java.lang.NullPointerException

at Student.(Student.java:9)

at TestStudent.main(TestStudent.java:5)

Updated error

最佳答案

使用quizList.add(qz[i]);而不是quizList.set(i, qz[i]);当您想要将对象设置到特定位置时,您的列表为空。

documentation of set :

IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())

您的更新:

您删除了行 quizList = new ArrayList<Integer>(5); 。它会初始化您的列表。您需要将此行固定到位,否则没有可以添加对象的列表。一个NullPointerException始终意味着当您想要读取/写入对象时尚未创建对象。

关于java - 相对于构造函数抛出越界运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23737638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com