add方法即使超出了arraylist的大小,也会继续添加

时间:2016-07-01 22:29:39

标签: java arrays list

import java.util.ArrayList;
import java.util.Scanner;

/*Create a program that keeps track of specific information for Students. The information stored should be the following:
        First Name, Last Name, Major, GPA, UIN, NetID, Age, Gender,
        For this simple program we will only need to store 10 students in an ArrayList. 
        Your students should be stored in an object called Student.
        You should be able to add, display and remove Students in the ArrayList.
        You will submit 2 files for grading: Lab4.java and Student.java*/

public class Lab4 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        ArrayList<Student> newStudents = new ArrayList<Student>();
        // ArrayList<Student> newStudents = new ArrayList<Student>(10); tried this as well, but doesn't work.

        System.out.println("Welcome to the Student Interface!.");
        System.out.println("Please select a number from the options below \n");

        while (true) {
            // Give the user a list of their options
            System.out.println("1: Add a student to the list.");
            System.out.println("2: Remove a student from the list.");
            System.out.println("3: Display all students in the list.");
            System.out.println("0: Exit the student interface.");

            // Get the user input

            int userChoice = input.nextInt();
            switch (userChoice) {
            case 1:
                addStudents(newStudents);
                break;
            case 2:
                removeStudent(newStudents);
                break;
            case 3:
                displayStudent(newStudents);
                break;
            case 0:
                System.out.println("Thank you for using the student interface. See you again soon!");
                System.exit(0);
            }
        }
    }

    public static void addStudents(ArrayList<Student> newStudents) {

        Scanner input = new Scanner(System.in);
        boolean student_added = false;
        // TODO: Add a student that is specified by the user

        System.out.println("Please enter first name: ");
        String firstName = input.next();
        System.out.println("Please enter last name: ");
        String lastName = input.next();
        System.out.println("Please enter major: ");
        String Major = input.next();
        System.out.println("Please enter GPA: ");
        String GPA = input.next();
        System.out.println("Please enter UIN: ");
        String UIN = input.next();
        System.out.println("Please enter NetID: ");
        String NetID = input.next();
        System.out.println("Please enter Age: ");
        String Age = input.next();
        System.out.println("Please enter Gender: ");
        String Gender = input.next();

        for (int i = 1; i < 5; i++) { // ( I have also tried i<newStudents.size(). Didn't work.Thank you in advance!)
            newStudents.add(new Student(firstName, lastName, Major, GPA, UIN, NetID, Age, Gender));
            student_added = true;
            break;
        }

        if (student_added) {
            System.out.println("Student Added");
            System.out.println();

        } else {

            System.out.println("\n Student Interface is full!");

        }

    }

    private static void displayStudent(ArrayList<Student> newStudents) {
        // TODO Auto-generated method stub

        for (Student e : newStudents) {
            System.out.println(e);
        }
    }

    private static void removeStudent(ArrayList<Student> newStudents) {
        Scanner input = new Scanner(System.in);

        System.out.println("Please, enter the UIN to remove the Student: ");
        String uin = input.nextLine();

        for (Student e : newStudents) {
            if (e.getUIN().equals(uin)) {
                newStudents.remove(e);
                System.out.println("Student removed");
                break;
            }

            else {
                System.out.println("Sorry, no such student with this " + uin + " " + "number exist");

            }

        }

    }

}

1 个答案:

答案 0 :(得分:1)

正在将Student实例添加到列表中,并且不检查数组的大小是否大于或等于10.您正在检查i变量的值,该变量是在你输入for循环。

在这种情况下,for循环不适合此作业。

相反,请检查newStudents.size(),如果不超过最大值,请将学生添加到列表中。

例如:

if (newStudents.size() <= 10) { // check the size of the array [see note 1]
    newStudents.add(new Student(firstName, lastName, Major, GPA, UIN, NetId, Age, Gender));
    System.out.println("Student added\n");
} else {
    System.out.println("\n Student interface is full!");
}

注1:另外,如果10是程序顶部的常量(定义为public static const MAX_STUDENTS = 10;),那么最好使代码更易于维护。请参阅this question了解幻数是什么。