java - 使用空数组创建方法

标签 java arrays inheritance polymorphism

感谢 stackoverflow 用户的一些在线帮助,这里是对注册和删除学生方法的编辑,以添加到学生数组设置中。现在只需让其他方法也能发挥作用即可。对这些的任何见解都会有所帮助,特别是对于这些点的速度和效率。

导入java.util.Scanner;

公开课学校{

private Student[] theStudents;

public School() {
    this.theStudents = new Student[] { null };// needs to start out as empty

}

/*
 * next two methods would allow a user to add or drop a student into the
 * student array for the school Also with Enroll student, should be able to
 * assign a class to the student, i.e. Calculas, history, etc
 */
public void enrollStudent(Student newStudent) {
    Student newStudents[] = new Student[theStudents.length+1];
    for (int i = 0; i < theStudents.length; i++)
    {
        newStudents[i] = theStudents[i];
    }
    newStudents[theStudents.length] = newStudent;
    theStudents = newStudents;
}

public void dropStudent(Student dropStudent) {
    Student newStudents[] = new Student[theStudents.length-1];
    for (int i = 0; i > theStudents.length; i--)
    {
        newStudents[i] = theStudents[i];
    }
    newStudents[theStudents.length] = dropStudent;
}

// add Test Score for a student
public double addTestScore(String newStudent, double testScore) {
    testScores[posi] = testScore;
}

/*
 * count the number of students in a given class, not the school
 */
public int countClassSize(String course) {

}

// get average average score of a given class
public double averageAverageScore(String course) {

}

/*
 * add a programming language to only CS students at the school Will need to
 * use the instanceof for proper type casting
 */
public String addProgrammingLanguage(String studentName, String programLanguage) {

}

/*
 * Count the number of students in the school that know a certain
 * programming language, again will need to typecast properly
 */
public int numberThatKnowLanguage(String programLanguage) {

}

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner input = new Scanner(System.in);
    String dropStudent, course;

    System.out.println("Welcome to the School. Please select " + "Option 1 to enroll a regular student, \n "
            + "Option 2 to enroll a CompSci student, \n" + "Option 3 to drop a student, \n"
            + "Option 4 to add test score or programming language, or \n" + "Option 5 to perform class analytics.");
    int Operation = input.nextInt();

    /*
     * Simple UI to add and drop students, will need to set the operation to
     * call the add and drop students to fit them to the Student body array
     * will need to make these two options loop until the user is satisfied
     * with the size of the student body
     */
    if (Operation == 1) {
        System.out.println("Enter the # of regular students that you want to add to the school.");
        int addStudents = input.nextInt();
        /*
         *  Possibly create some type of input array to 
         *  make it easier to enter the students' names
         */
        System.out.println("Please enter the name and course of the student you are enrolling:");
        Student newRegularStudent = (String) input.next();
        course = input.next();
    }

    else if (Operation == 2) {
        System.out.println("Enter the # of CompSci students that you want to add to the school.");
        int addStudents = input.nextInt();
        /*
         * Possibly create some type of input array to make it easier to
         * enter the students' names
         */
        System.out.println("Please enter the name and course of the student you are enrolling:");
        Student newCompSciStudent = (String) input.next();
        course = input.next();
    }

    else if (Operation == 3) {
        System.out.println("Enter the # of students that you want to drop from the school.");
        int dropStudents = input.nextInt();
        /*
         * Possibly create some type of input array to make
         *  it easier to enter the students' names
         */
        System.out.println("Please enter the name of the student you wish to drop from the school:");
        dropStudent = (String) input.next();

        /*
         * After the first two operations, will need to build to the UI to
         * call the other five methods, will need to make it into a
         * while/for loop so user can continue to add information as needed.
         */
    }

    else if (Operation == 4) {
        System.out.println("Enter the # for what you want to add to a student's records."
                + "Enter 1 to enter a test score\n   " + "Enter 2 to enter a programming language, enter 2.");
        int optionNum1 = input.nextInt();
        /*
         *  Possibly create some type of input array 
         *  to make it easier to enter the students' names
         */
        if (optionNum1 == 1) {

        } else if (optionNum1 == 2) {

        }
    }

    else if (Operation == 5) {
        System.out.println("This is the analytics section of this program.\n");

        System.out.println("Enter the # for which of the following analytics options that you want performed: "
                + "Enter 1 to count the # of students for a particular class,\n "
                + "Enter 2 to calculate the average average score for all students take a particular course, or\n "
                + "Enter 3 to count the # of CompSciStudents.");
        int optionNum2 = input.nextInt();
        if (optionNum2 == 1) {
            System.out.println("Enter the course name that you want to know the # of students for.");
            course = input.next();
            Student classSize;
            classSize.countClassSize(course);
        }

        else if (optionNum2 == 2) {
            System.out.println("Enter the course name that you want \n"
                    + "to calculate the average average test value for.");
            course = input.next();
            Student testAvg;
            testAvg.averageAverageScore(course);
        }

        else if (optionNum2 == 3) {
            System.out.println("Count the # of CompSciStudents who know a \n"
                    + "particular programming language by entering that language name now.");
            String programLanguage = input.next();
            CompSciStudent csStudent;
            csStudent.knowsLanguage(programLanguage);
        }
    }
    input.close();
}

}

最佳答案

我不知道这是否是最好的方法,但如果我是你,我只会创建一个大一号或小一号的新数组,并在每次添加或删除时使用旧数组中的值填充它值(value)。例如:

public void enrollStudent(Student newStudent) {
    Students newStudents[] = new Student[theStudents.length+1];
    for (int i = 0; i < theStudents.length; i++)
    {
        newStudents[i] = theStudents[i];
    }
    newStudents[theStudents.length] = newStudent;
    theStudents = newStudents;
}

效率不是很高,但是嘿,听起来你并没有做任何需要 super 高效的事情。

关于java - 使用空数组创建方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36686317/

相关文章:

java - 如何检查随机生成的数组中的每个 int 是否偶数,如果不是,则让它创建另一个随机数组?

Java之前如何用小输入屏幕抓取?

php - 如何在 MySQL IN 语句中的内爆数组中的字符串周围添加撇号?

r - 如何在R中连接两个数组

python - py.test 不收集不是从 'object' 继承的测试

java - Android:如何在 Room 中的多对多关系中插入数据(Java)

java - Spring将JSP中的数据绑定(bind)到Controller

javascript - JSON.parse in Javascript乱序选择

Python:从内部类访问外部私有(private)函数

c++ - 初始值设定项是基类名称时出现错误 'initializer does not name a non-static data member or base class'