Java 将学生添加到数组中

Java 将学生添加到数组中,java,arrays,Java,Arrays,我已经知道如何从用户那里收集数据,在我的数组中创建一个新的学生,但是我在将这些信息添加到数组中时遇到了困难。请告诉我如何在给定代码中将此数据添加为新的Student对象。请参考AddStudent方法 import java.util.Scanner; public class ArrayDemo { static Student[] students; private static void ViewStudents() { for (int i = 0

我已经知道如何从用户那里收集数据,在我的数组中创建一个新的学生,但是我在将这些信息添加到数组中时遇到了困难。请告诉我如何在给定代码中将此数据添加为新的Student对象。请参考
AddStudent
方法

import java.util.Scanner;

public class ArrayDemo {

    static Student[] students;

    private static void ViewStudents() {

        for (int i = 0; i < students.length; i++) {
            System.out.println(i + ") " + students[i].getLName() + ", " + students[i].getFName());
        }
    }

    private static void ViewDetails() {
        Scanner kb = new Scanner(System.in);

        int i;
        System.out.println("Who would you like to view?");
        ViewStudents();
        i = Integer.parseInt(kb.nextLine());
        System.out.println("ANum:\t\t" + students[i].getANum());
        System.out.println("\nAddress:\t" + students[i].address.getHouseNum() + " " + students[i].address.getStreet());
        System.out.println("\t\t" + students[i].address.getCity() + ", " + students[i].address.getState() + " " + students[i].address.getZip());
        System.out.println("\t\t" + students[i].address.getLine2());
    }

    private static void AddStudent() {
        Scanner kb = new Scanner(System.in);

        Student student = new Student();

        String FirstName;
        String LastName;
        int HouseNum;
        String Street;
        String City;
        String State;
        int Zip;
        String Line2;

        System.out.println("\tInput Information");
        System.out.println("\tFirst Name:");
        FirstName = kb.nextLine();
        System.out.println("\tLast Name:");
        LastName = kb.nextLine();
        System.out.println("\tHouse Number:");
        HouseNum = Integer.parseInt(kb.nextLine());
        System.out.println("\tStreet:");
        Street = kb.nextLine();
        System.out.println("\tCity:");
        City = kb.nextLine();
        System.out.println("\tState:");
        State = kb.nextLine();
        System.out.println("\tZip Code:");
        Zip = Integer.parseInt(kb.nextLine());
        System.out.println("\tExtra Information:");
        Line2 = kb.nextLine();

        System.out.println("\nStudent:\t" + LastName + ", " + FirstName);
        System.out.println("ANum:\t\t" + student.getANum());
        System.out.println("Address:\t" + HouseNum + " " + Street);
        System.out.println("\t\t" + City + ", " + State + " " + Zip);
        System.out.println("\t\t" + Line2);

        //students.setAddress( HouseNum, Street, City, State, Zip, Line2 );
        System.out.println("\tYour Student was Successfully Added");
    }

    private static void RemoveStudent() {
        Scanner kb = new Scanner(System.in);
        System.out.println("Who would you like to remove?");
        ViewStudents();

        for (int j = Integer.parseInt(kb.nextLine()); j < students.length - 1; j++) {
            students[j] = students[j + 1];
        }

        students[students.length - 1] = null;
    }

    public static void main(String[] args) {
        Scanner kb = new Scanner(System.in);

        int x = 40;
        //students = new Student[0];
        students = new Student[2];

        students[0] = new Student("Thomas", "Emily");
        students[1] = new Student("Bob", "Joe");
        students[0].address = new Address(6614, "White Sands ln", "Hixson", "Tennessee", 37343, "");
        students[1].address = new Address(66, "White  ln", "Hson", "Tealamabaee", 373873, "");

        do {
            System.out.println();
            System.out.println("Do you want to:");
            System.out.println("\t0) View Students");
            System.out.println("\t1) View Students' Details");
            System.out.println("\t2) Add a Student");
            System.out.println("\t3) Remove a Student");
            System.out.println("\t4) Exit");
            x = Integer.parseInt(kb.nextLine());

            switch (x) {

                case 0:
                    ViewStudents();
                    break;
                case 1:
                    ViewDetails();
                    break;
                case 2:
                    AddStudent();
                    break;
                case 3:
                    RemoveStudent();
                    break;
                case 4:

                    break;
                default:
            }
        } while (x != 4);
    }
}
import java.util.Scanner;
公共类ArrayDemo{
静态学生[]学生;
私人静态void ViewStudents(){
for(int i=0;i
相信我,如果不知道数组中当前有多少条记录,就无法将记录添加到数组中。如果不想让另一个静态变量跟踪记录数,则必须在数组中循环,直到它为null

static int numOfStudents = 0; //declare outside your main

public static void AddStudent()
{
    Scanner scn = new Scanner( System.in );

    if (numOfStudents < students.length){
        System.out.println("Enter last name:");
        String ln = scn.nextLine():
        System.out.println("Enter first name:");
        String fn = scn.nextLine():

        Student stud = new Student(ln, fn);
        students[numOfStudents] = stud;
        numOfStudents ++;
    }
}
static int=0//在你的主要任务之外申报
公共静态void AddStudent()
{
扫描仪scn=新扫描仪(System.in);
如果(学生人数<学生长度){
System.out.println(“输入姓氏:”);
字符串ln=scn.nextLine():
System.out.println(“输入名字:”);
字符串fn=scn.nextLine():
学生成绩=新生(ln,fn);
学生[学生]=种马;
NUMOF++;
}
}
您喜欢的替代解决方案。但在我看来,这是不好的

public static void AddStudent()
{
    Scanner scn = new Scanner( System.in );

    int index=0;

    while(x<students.length && student[x] != null)
        index++; //get position to add new student

    System.out.println("Enter last name:");
    String ln = scn.nextLine():
    System.out.println("Enter first name:");
    String fn = scn.nextLine():

    Student stud = new Student(ln, fn);
    students[index] = stud;      
}
publicstaticvoidaddstudent()
{
扫描仪scn=新扫描仪(System.in);
int指数=0;

虽然(x从我看来,您使用了错误的数据结构。您似乎认为添加动态数量的
Student
s和
ArrayList
Array
更好、更合适。然后您可以简单地使用
add
方法

import java.util.Scanner;

public class ArrayDemo {

    static Student[] students;

    private static void ViewStudents() {

        for (int i = 0; i < students.length; i++) {
            System.out.println(i + ") " + students[i].getLName() + ", " + students[i].getFName());
        }
    }

    private static void ViewDetails() {
        Scanner kb = new Scanner(System.in);

        int i;
        System.out.println("Who would you like to view?");
        ViewStudents();
        i = Integer.parseInt(kb.nextLine());
        System.out.println("ANum:\t\t" + students[i].getANum());
        System.out.println("\nAddress:\t" + students[i].address.getHouseNum() + " " + students[i].address.getStreet());
        System.out.println("\t\t" + students[i].address.getCity() + ", " + students[i].address.getState() + " " + students[i].address.getZip());
        System.out.println("\t\t" + students[i].address.getLine2());
    }

    private static void AddStudent() {
        Scanner kb = new Scanner(System.in);

        Student student = new Student();

        String FirstName;
        String LastName;
        int HouseNum;
        String Street;
        String City;
        String State;
        int Zip;
        String Line2;

        System.out.println("\tInput Information");
        System.out.println("\tFirst Name:");
        FirstName = kb.nextLine();
        System.out.println("\tLast Name:");
        LastName = kb.nextLine();
        System.out.println("\tHouse Number:");
        HouseNum = Integer.parseInt(kb.nextLine());
        System.out.println("\tStreet:");
        Street = kb.nextLine();
        System.out.println("\tCity:");
        City = kb.nextLine();
        System.out.println("\tState:");
        State = kb.nextLine();
        System.out.println("\tZip Code:");
        Zip = Integer.parseInt(kb.nextLine());
        System.out.println("\tExtra Information:");
        Line2 = kb.nextLine();

        System.out.println("\nStudent:\t" + LastName + ", " + FirstName);
        System.out.println("ANum:\t\t" + student.getANum());
        System.out.println("Address:\t" + HouseNum + " " + Street);
        System.out.println("\t\t" + City + ", " + State + " " + Zip);
        System.out.println("\t\t" + Line2);

        //students.setAddress( HouseNum, Street, City, State, Zip, Line2 );
        System.out.println("\tYour Student was Successfully Added");
    }

    private static void RemoveStudent() {
        Scanner kb = new Scanner(System.in);
        System.out.println("Who would you like to remove?");
        ViewStudents();

        for (int j = Integer.parseInt(kb.nextLine()); j < students.length - 1; j++) {
            students[j] = students[j + 1];
        }

        students[students.length - 1] = null;
    }

    public static void main(String[] args) {
        Scanner kb = new Scanner(System.in);

        int x = 40;
        //students = new Student[0];
        students = new Student[2];

        students[0] = new Student("Thomas", "Emily");
        students[1] = new Student("Bob", "Joe");
        students[0].address = new Address(6614, "White Sands ln", "Hixson", "Tennessee", 37343, "");
        students[1].address = new Address(66, "White  ln", "Hson", "Tealamabaee", 373873, "");

        do {
            System.out.println();
            System.out.println("Do you want to:");
            System.out.println("\t0) View Students");
            System.out.println("\t1) View Students' Details");
            System.out.println("\t2) Add a Student");
            System.out.println("\t3) Remove a Student");
            System.out.println("\t4) Exit");
            x = Integer.parseInt(kb.nextLine());

            switch (x) {

                case 0:
                    ViewStudents();
                    break;
                case 1:
                    ViewDetails();
                    break;
                case 2:
                    AddStudent();
                    break;
                case 3:
                    RemoveStudent();
                    break;
                case 4:

                    break;
                default:
            }
        } while (x != 4);
    }
}
请记住,
数组
的大小是不可变的(它不能更改),因此在这种情况下,您需要的肯定是某种类型的
列表
,可能是
数组列表

如果坚持使用数组,则需要跟踪
private static void AddStudent() {

   // ... the code where you obtain user information

   if(index < students.length) {  // make sure there is room to add the user
       Student student = new Student(/* add user information into the constructor */);
       students[index] = student; // add the user at the index 
       index++;                   // update the index
   } else {
       System.err.println("No more room for students");
   }
}
Student student = new Student();
student.firstName = firstName;
// ...
student.setFirstName(firstName);
static int ARRAY_MAX = 10;
static Student[] students;
static int numStudents;
...
students = new Student[ARRAY_MAX];
numStudents = 0;
students[numStudents] = new Student();
numStudents++;