【问题标题】:Generate array of students (Java)生成学生数组(Java)
【发布时间】:2017-11-30 17:56:04
【问题描述】:

我需要生成具有这些属性的学生数组: 姓名、姓氏、专长、课程、组别。

我写了学生类并尝试创建 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

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

【问题讨论】:

  • main 中没有 student 变量,那么它是从哪里来的呢?它是一个字段吗?
  • @Ramanlfc:第 9 行:D
  • 需要来自学生班@Ramanlfc
  • 是在Student 中定义的main
  • 废弃所有损坏的代码并重新开始。您不应该在 Student 类中对单个学生的创建进行硬编码。相反,它应该被创建以供其他类用来创建单个学生对象。

标签: java arrays


【解决方案1】:

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

该构造函数将如下所示:

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

您还删除了Student student; 行。

然后你写:

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

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

【讨论】:

  • 但在任务中我需要创建班级学生并生成包含 5 个学生的数组
  • 是的,你的课程。这基本上是你的前 6 行。在这个类中,您需要添加一个Constructor,用于构造instances。然后在您的main-function 中,您使用该构造函数 5 次来创建 5 个添加到您的 mStudent-array 的实例。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    学生班级应该只是一个蓝图。当您创建多个“学生”时,您应该从 main() 中执行此操作。即,

    学生 student1 = new Student();

    学生 student2 = new Student();

    ...

    学生 student5 = new Student();

    然后将它们添加到您的数组中,就像现在一样,但使用实际的学生对象:

    mStudents.add(student1);

    mStudents.add(student2);

    ...

    mStudents.add(student5);

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

    【讨论】:

      【解决方案4】:

      首先,您需要创建一个名为 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");
      

      最后,您将能够通过这种方式创建一个学生数组列表:

      //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);
      

      希望对你有帮助!

      【讨论】:

      • 我不认为package ... 行有帮助。使用您的答案的人可能会在未来几年将确切的行添加到每个文件中:D
      • 哎呀,我没注意到!这可能是在互联网上传播我的名字的一种偷偷摸摸的方式;)谢谢您的评论,我会纠正它:)
      猜你喜欢
      • 2021-05-31
      • 1970-01-01
      • 2014-11-30
      • 1970-01-01
      • 2022-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多