正在从数组

时间:2017-09-18 02:43:44

标签: java

我的程序没有任何错误,但错误的学生正在从我的课程(数组)中删除,我不知道为什么。如果我将名称更改为第3名学生(James),我会在dropStudent行上出现NPE错误。我认为这可能与我的dropStudent方法有关,但我认为它应该循环使用索引等于' i'当该索引等于该名称时,它将变为空,而另外两名学生则为空。

我会将下面的代码和我的输出一起发布。

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;
    }

}

输出:

The students in the course cs216 are:
William Angela

3 个答案:

答案 0 :(得分:1)

dropStudent方法中:

    if (course[i].equals("William")) {

所以这种方法只会放弃威廉,无论哪个学生真正被传入其中。

此外,您正在遍历students数组,但dropStudent仅从course数组中移除学生。

答案 1 :(得分:1)

dropStudent修改course数组,但不修改您要打印的student数组。更大的问题是,您只是将null分配给要删除的学生,但减少学生人数,因此即使您从courses打印,也无法获得正确的输出。

如果学生阵列是:

William | Angela | James : numStudents = 3

然后放弃威廉制作阵列:

null | Angela | James : numStudents = 2

现在,如果您尝试打印学生,您的代码将打印前两个元素,即null和Angela。可能不是你想要的。

当您放弃学生时,您应该转移元素,以便数组看起来像:

Angela | James | null : numStudents = 2

在放弃威廉之后,或者不是重新发明轮子,只需使用ArrayList

答案 2 :(得分:1)

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;
        }
    }       
}