Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 创建实例并将它们存储在数组中_Java_Arrays - Fatal编程技术网

Java 创建实例并将它们存储在数组中

Java 创建实例并将它们存储在数组中,java,arrays,Java,Arrays,我有一个student类,创建的每个student实例都需要存储在一个数组中。属性可以在程序中给定,也可以从用户输入中读取。如何扩展我的主方法来存储它?我被卡住了 这是我的学生班级代码: public class student { int studentID; String studentName,gender,course; /** set the student name * @param studentName */ public void

我有一个student类,创建的每个student实例都需要存储在一个数组中。属性可以在程序中给定,也可以从用户输入中读取。如何扩展我的主方法来存储它?我被卡住了

这是我的学生班级代码:

public class student {

    int   studentID;
    String studentName,gender,course;
 /** set the student name
  * @param studentName
 */
 public  void        setName(String studentName){
this.studentName = studentName;
 }
  /**
  * set student ID number
  * @param studentID 
   */
   public  void        setNewId(int studentID){
   this.studentID = studentID;
  }
   /**
   * Set student gender
   * @param gender 
   */
   public void setGender(String gender){
  this.gender = gender;
 }
 /**
  * set the course
  * @param course 
  */
   public  void        setCourse(String course){
   this.course = course;
 }
 /**
  *Gets the Student's ID Number.
  *@return IdNumber Student ID Number.
  */
  public  int      getIdNumber(){
return studentID;
 }

  /**
  *Gets the Student's Name.
  *@return studentName Student Name.
  */
 public  String      getName(){
return studentName;
 }
 /**
  * Get student gender
  * @return 
   */
 public String getGender(){
  return gender;
 }
 /**
  *Gets the Student's Course.
  *@return course Student Course.
  */

  public  String     getCourse(){
return course;
  }
  /**
  *Prints Student Informations.
  *@return Student ID Number, name, gender and course.
    */

  public void printStudent(){
 System.out.print(studentID+""+studentName+""+gender+""+course);
}
}
这是我的主类,其中包含需要数组的main方法:

public class SMSMain {
    /**
     * 
     * @param args
     */
    public static void main(String[] args) {
    // Create an instance of student object

    student a = new student();
    a.studentName = "Maria";
    a.studentID = 1236;
    System.out.println("Student Name:" + a.studentName);
    System.out.println("Student ID:" + a.studentID);
}
}

您需要启动数组并在循环中创建对象

   Student[] students;
    System.out.println("Enter number of students :");


    DataInputStream in = new DataInputStream(System.in);
    int n = in.readInt();
    students = new Student[n];
    for(int i=0; i<n ;i++) {
        students[i] = new Student();
        System.out.println("Name:");
        students[i].setName(in.readLine());
        System.out.println("Id:");
        students[i].setNewId(in.readInt());
     }

首先在main方法中声明并初始化Student[]数组注意,类的第一个字母应该大写。然后用学生实例填充它。您应该向我们展示您的尝试,以便我们能够更好地了解您需要的帮助。请从您的帖子中删除所有与您大部分代码中的问题不直接相关的代码。顺便说一句,您不应该在家庭作业问题中发布答案。虽然我们知道用户没有说这是家庭作业,但很明显,他正在学习,我们不应该告诉他答案,而是应该帮助他用自己的腿找到答案。谢谢tsatiz,这不是家庭作业,只是我在做的书本练习,因为我在自学java。