gpt4 book ai didi

java - 为什么这个数组会不一致地越界?

转载 作者:行者123 更新时间:2023-12-02 06:39:59 25 4
gpt4 key购买 nike

这是我的一个学生类,它创建一个具有姓名、年级和 ID 号的学生。 Student 用作 TreeMap 中的键,而学生的成绩用作值。我在实现Comparable时编写了compareTo,但是在输入第一个学生时会弹出此错误:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at Student.<init>(Student.java:25)
at Main.main(Main.java:50)
Java Result: 1

我认为这是由于将学生的名字分割成一个 String[] 造成的,但我测试了那些调用名字和姓氏的行,以及分割名字的行,它们似乎都按预期工作,除非在compareTo 中调用它们。

任何帮助将不胜感激。

public class Student implements Comparable<Student>{
private String name, grade, first, last;
private String[] stuName;
private int id;

/**
* Constructs a new Student with a name, ID Number, and a grade.
* @param n name
* @param i ID Number
* @param g grade
*/
public Student(String n, int i, String g){
name = n;
id = i;
grade = g;
stuName = n.split(" ");
first = stuName[0];
last = stuName[1];
}

/**
* Compares Students. First by last name, then by first if the last names
* are the same. If the first names are also the same, the students are
* compared by their ID Numbers.
* @return the value upon comparing the proper property of the Student
*/
@Override
public int compareTo(Student other){
if (last.equals(other.getLast())){
if (first.equals(other.getFirst())){
return id - other.getID();
}else
return first.compareTo(other.getFirst());
}
return last.compareTo(other.getLast());
}

/**
* Changes the student's current grade.
* @param g new grade
*/
public void changeGrade(String g){
grade = g;
}

/**
* Returns student's name.
* @return name
*/
public String getName(){
return name;
}

/**
* Returns student's first name.
* @return first name
*/
public String getFirst(){
return first;
}

/**
* Returns student's last name.
* @return last name
*/
public String getLast(){
return last;
}

/**
* Returns the student's grade.
* @return grade
*/
public String getGrade(){
return grade;
}

/**
* Returns the student's ID Number
* @return id number
*/
public int getID(){
return id;
}

测试人员:

public class Main {
public static void main(String[] args) throws InterruptedException{
Map<Student, String> students = new TreeMap();
Scanner in = new Scanner(System.in);
System.out.println("How many students do you want to add?");
int numStudents = in.nextInt();
String name, grade;
int id;


for (int i = 1; i < numStudents; i++){
System.out.println("Name of student " + i + "?");
name = in.nextLine();
in.nextLine();

System.out.println("Grade of " + i + "?");
grade = in.nextLine();

System.out.println("ID Number of " + i + "?");
id = in.nextInt();

Student s = new Student(name, id, grade);
students.put(s, s.getGrade());
}


System.out.println("How many students do want to remove");
int remStudents = in.nextInt();

for (int i = 0; i < remStudents; i++){
System.out.println("ID?");
int remID = in.nextInt();

for (Student s : students.keySet()){
if (s.getID() == remID){
students.remove(s);
}
}
}


System.out.println("How many grades do you want to change?");
int changeGrades = in.nextInt();

for (int i = 0; i < changeGrades; i++){
System.out.println("ID?");
int foo = in.nextInt();
System.out.println("New grade?");
String newGrade = in.nextLine();

for (Student s : students.keySet()){
if (s.getID() == foo){
s.changeGrade(newGrade);
}
}
}

String printout = "";
for (Student s : students.keySet()){
printout += s.getLast() + ", " + s.getFirst() + " (" + s.getID() + "): " + s.getGrade() + "\n";
}
System.out.println(printout);
}
}

最佳答案

可能是因为您有两个不同的循环,具有不同的索引:

在你的一个循环中,你从 1 开始,因此你还缺 1 个学生:

for (int i = 1; i < numStudents; i++){

在删除循环中,您有一个从 0 开始的索引:

for (int i = 0; i < remStudents; i++){

我怀疑,你认为你添加了 2 个学生,但实际上你只有一个(在索引 0 处),因此你的索引越界异常。

编辑,OP为异常添加了一个“完整”堆栈,上面的答案与OP的问题无关......

答案 2:根据您修改后的堆栈/异常编辑,唯一可能的答案是学生姓名中没有空格......您关于总是有空格的断言根本不正确......

您可能需要在拆分中添加计数限定符,以便在任何无效输入上都会得到一个空字符串:

stuName = n.split(" ", 2);

关于java - 为什么这个数组会不一致地越界?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19237662/

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