生成一系列学生(Java)

时间:2017-11-30 17:56:04

标签: java arrays

我需要生成具有这些属性的学生数组: 姓名,姓氏,专业,课程,Groupe。

我写了班级学生并尝试创建5名学生

这是代码

public class Student {
String Name;
String Surname;
String Course;
String Groupe;
String Speciality;


Student student = new Student();

public Student getStudent1() {
    student.Name = "Anastasia";
    student.Surname = "Enina";
    student.Speciality = "Programmer";
    student.Course = "4";
    student.Groupe = "1B";
    return student;
}

public Student getStudent2(){
    student.Name = "Anastasia";
    student.Surname = "Enina";
    student.Speciality = "Programmer";
    student.Course = "4";
    student.Groupe = "1B";
    return student;
}
public Student getStudent3(){
    student.Name = "Eugene";
    student.Surname = "Sukhomlyn";
    student.Speciality = "Economist";
    student.Course = "3";
    student.Groupe = "2B";
    return student;
}
public Student getStudent4(){
    student.Name = "Victor";
    student.Surname = "Sologub";
    student.Speciality = "Designer";
    student.Course = "5";
    student.Groupe = "2A";
    return student;
}
public Student getStudent5(){
    student.Name = "Serhiy";
    student.Surname = "Koshelnick";
    student.Speciality = "Audit";
    student.Course = "1";
    student.Groupe = "4C";
    return student;
}

}

主要是我需要将这些学生添加到数组

所以我尝试了这段代码

 public static void main(String[] args) {
    ArrayList<Student> mStudents = new ArrayList<>();

    mStudents.add(student);
}

但我有错误cannot resolve symbol student

我需要如何正确编写代码?

4 个答案:

答案 0 :(得分:0)

您将删除所有这些函数:getStudentX() - 而是编写一个构造函数,为每个属性接受一个参数。

该构造函数看起来像这样:

public Student(String name, String ...) {
    this.name = name;
    ...
}

您也放弃了Student student;行。

然后你写:

mStudent.add(new Student(arg1, arg2, ....));

创建一个添加到数组中的新Student实例。

答案 1 :(得分:0)

您应该创建包含所有实例变量的学生类,包含所有变量的构造函数,每个变量的优选getter和setter。然后,从您的main(测试类)创建和ArrayList并包括每个学生。

ArrayList<Student> mStudents = new ArrayList<>();
mStuddents.add(new Student(Anastasia, Enina, Programmer, 4, 1B);

答案 2 :(得分:0)

学生班应该只是一个蓝图。当您创建多个&#34;学生&#34;时,您应该从main()执行此操作。即,

学生student1 =新学生();

学生student2 =新学生();

...

学生student5 =新学生();

然后将它们添加到您的数组中,按照您现在的方式执行,但使用实际的学生对象:

mStudents.add(student1);

mStudents.add(STUDENT2);

...

mStudents.add(student5);

您需要重写您的学生课程,因为您现在拥有的课程并不真正有意义。 请查看此链接以供参考: https://beginnersbook.com/2013/03/constructors-in-java/

答案 3 :(得分:0)

首先,您需要创建一个名为Student的类。 然后,您将向该类添加构造函数,以便能够实例化新的Student。

public class Student {
 private String name;
 private String surname;
 private String course;
 private String groupe;
 private String speciality;

 public Student(String name,
               String surname,
               String course,
               String groupe,
               String speciality)
 {
     this.name = name;
     this.surname = surname;
     this.course = course;
     this.groupe = groupe;
     this.speciality = speciality;
 }

}

编写构造函数后,您将能够实例化一个新对象:

 Student student1 = new Student("name1", "surname1", "course1", "groupe1", "speciality1");

Finnaly,你将能够以这种方式创建一个学生的arraylist:

//instantiation of multiple students
Student student1 = new Student("name1", "surname1", "course1", "groupe1", "speciality1");
//..
Student student5 = new Student("name5", "surname5", "course5", "groupe5", "speciality5");

// Creation of an ArrayList
ArrayList<Student> mStudents = new ArrayList();

// Adding students to the ArrayList
mStudents.add(student1);
//...
mStudents.add(student5);
希望这会对你有帮助!