Java add方法即使超过arraylist的大小也会继续添加

Java add方法即使超过arraylist的大小也会继续添加,java,arrays,list,Java,Arrays,List,正在将学生实例添加到列表中,不检查数组的大小是否大于或等于10。您正在检查输入for循环时创建的i变量的值 在这种情况下,for循环不是适合此作业的工具 相反,检查newStudents.size,如果未超过最大值,则将该学生添加到列表中 例如: import java.util.ArrayList; import java.util.Scanner; /*Create a program that keeps track of specific information for Student

正在将学生实例添加到列表中,不检查数组的大小是否大于或等于10。您正在检查输入for循环时创建的i变量的值

在这种情况下,for循环不是适合此作业的工具

相反,检查newStudents.size,如果未超过最大值,则将该学生添加到列表中

例如:

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:作为旁白,最好是在程序顶部的10是一个常数,定义为publicstaticconstmax_STUDENTS=10;使代码更易于维护。看看什么是神奇的数字。

你希望发生什么?请发表一篇文章,包括你的问题。谢谢!我想出来了。假设打印消息“学生界面已满”。我补充道,如果newStudents.sizeArrayList没有最大限制。大小是元素的当前数量。容量不是它在不调整自身大小的情况下可以处理的元素数。@SotiriosDelimanolis,对不起,我是stackoverflow的新手,仍然习惯于在线寻求帮助以及编码。我感谢你的反馈。谢谢你的耐心,我知道了。我用了同样的方法。感谢您的反馈!
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!");
}