Java 正在从数组中删除错误的对象
我的程序没有任何错误,但是错误的学生被从我的课程中删除(数组),我不知道为什么。如果我把名字改成第三个学生(James),我会在dropStudent行上得到一个NPE错误。我认为这可能与我的dropStudent方法有关,但我认为它应该在整个课程中循环,索引等于'I',当该索引等于名称时,它将变为null,剩下的两个学生 我将发布下面的代码以及我的输出Java 正在从数组中删除错误的对象,java,Java,我的程序没有任何错误,但是错误的学生被从我的课程中删除(数组),我不知道为什么。如果我把名字改成第三个学生(James),我会在dropStudent行上得到一个NPE错误。我认为这可能与我的dropStudent方法有关,但我认为它应该在整个课程中循环,索引等于'I',当该索引等于名称时,它将变为null,剩下的两个学生 我将发布下面的代码以及我的输出 package reviseCourse; import java.util.*; public class ReviseCourse {
package reviseCourse;
import java.util.*;
public class ReviseCourse {
public static void main(String[] args) {
// TODO Auto-generated method stub
// Create new course to enroll students in
ReviseCourse cs216 = new ReviseCourse("cs216");
// Add 3 students to the course
cs216.addStudent("William");
cs216.addStudent("Angela");
cs216.addStudent("James");
// Drop the student William from the course
cs216.dropStudent("William");
// Print course name and students individually by looping through the numberOfStudents
System.out.println("The students in the course " + cs216.getCourseName() + " are:");
for (int i = 0; i < cs216.getNumberOfStudents(); i++) {
System.out.print(students[i] + " ");
}
}
private String courseName;
private static String[] students = new String[100];
private String[] course = new String[students.length + 1];
private int numberOfStudents;
public void populateCourse() {
for (int i = 0; i < students.length; i++) {
course[i] = students[i];
System.out.println(course[i]);
}
}
public ReviseCourse(String courseName) {
this.courseName = courseName;
}
public void addStudent(String student) {
for (int i = 0; i < students.length; i++) {
course[i] = students[i];
}
students[numberOfStudents] = student;
numberOfStudents++;
}
public String[] getStudents() {
return students;
}
public int getNumberOfStudents() {
return numberOfStudents;
}
public String getCourseName() {
return courseName;
}
public void dropStudent(String student) {
for (int i = 0; i < course.length; i++) {
if (course[i].equals("William")) {
course[i] = null;
numberOfStudents--;
break;
}
}
}
// Deletes all students from the course
public void clear() {
numberOfStudents = 0;
}
}
在您的
dropStudent
方法中:
if (course[i].equals("William")) {
所以这个方法只会让William失望,不管哪个学生真的通过了
此外,您正在通过
学生
数组进行循环,但dropStudent
仅从课程
数组中删除学生。在dropStudent
方法中:
if (course[i].equals("William")) {
所以这个方法只会让William失望,不管哪个学生真的通过了
此外,您在
学生
数组中循环,但是dropStudent
仅从课程
数组中删除学生。dropStudent
修改课程
数组,而不是打印的学生
数组。更大的问题是,您只是将null
分配给要删除的学生,但会减少学生人数,因此即使您从课程
打印,也无法获得正确的输出
如果学生数组为:
William | Angela | James : numStudents = 3
然后,放下William就可以生成数组:
null | Angela | James : numStudents = 2
现在,如果您尝试打印学生,您的代码将打印前两个元素,即null
和Angela。可能不是你想要的
当您放弃学生时,您应该在元素上进行转换,以便数组看起来像:
Angela | James | null : numStudents = 2
放下William后,或者不用重新设计轮子,只需使用
ArrayList
dropStudent
即可修改课程
数组,而不是打印的学生
数组。更大的问题是,您只是将null
分配给要删除的学生,但会减少学生人数,因此即使您从课程
打印,也无法获得正确的输出
public void dropStudent(String student) {
for (int i = 0; i < course.length; i++) {
if (course[i].equals(student) {
course[i] = null;
numberOfStudents--;
//can re order to keep the array tidy, ie go with for if you find a
//null then set it to be the next value in the array; thus will keep
//array management more efficient
break;
}
}
}
如果学生数组为:
William | Angela | James : numStudents = 3
然后,放下William就可以生成数组:
null | Angela | James : numStudents = 2
现在,如果您尝试打印学生,您的代码将打印前两个元素,即null
和Angela。可能不是你想要的
当您放弃学生时,您应该在元素上进行转换,以便数组看起来像:
Angela | James | null : numStudents = 2
放下William后,或者不用重新发明轮子,只需使用ArrayList
public void dropStudent(String student){
public void dropStudent(String student) {
for (int i = 0; i < course.length; i++) {
if (course[i].equals(student) {
course[i] = null;
numberOfStudents--;
//can re order to keep the array tidy, ie go with for if you find a
//null then set it to be the next value in the array; thus will keep
//array management more efficient
break;
}
}
}
for(int i=0;i
公共学生(字符串学生){
for(int i=0;i
是的,但是在输出中你可以看到他仍然在那里,但是詹姆斯留在那里。詹姆斯仍然在学生
数组中。你只是在循环中看不到他,因为你只循环到学生数
,减少了一个。是的,但是在输出中你可以看到他仍然在那里,但是詹姆斯留在数组中。詹姆斯仍然在中>学生
数组。你只是看不到他在那个循环中,因为你只循环了numberOfStudents
,它减少了一个。