简体   繁体   English

如何从java中的类创建大量实例

[英]How do I create a lot of instances from a class in java

I am very new to java and I am not sure what i need to use in order to accomplish my goal... or I am not sure how to search for it on google or on stackoverflow... 我是java的新手,我不确定我需要使用什么来实现我的目标...或者我不确定如何在谷歌或stackoverflow上搜索它...

Suppose I have a class Student that has a method to get/set their name, and their major. 假设我有一个班级学生,有一个方法来获取/设置他们的名字,以及他们的专业。 I also have another class Major which I can create an instance and add students that belong to this major in an ArrayList. 我还有另一个类Major,我可以创建一个实例,并在ArrayList中添加属于这个专业的学生。

I have a Scanner so that the student can input their details... 我有一台扫描仪,学生可以输入他们的详细信息......

I would write it like this in my Student class with a main: 我会在我的学生班中用这样的主题写这样的:

    package exp;
import java.util.Scanner;
public class Student {

    private String name, major;
    private int id;

    public Student(){
    }
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
    public void setMajor(String major){
        this.major = major;
    }
    public String getMajor(){
        return major;
    }    
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        Student student1 = new Student();
        System.out.println("Enter name: ");
        student1.setName(scan.next());
        System.out.println("Enter major: ");
        student1.setMajor(scan.next());


        Major major1 = new Major();
        major1.addStudents(student1);
        System.out.println("List of students in major1: ");
        major1.getStudents(); 
    }
}

in my Major class: 在我的专业课:

package exp;
import java.util.ArrayList;
public class Major {

private ArrayList<Student> students;

public Major(){
    students = new ArrayList<Student>();
}

public void addStudents(Student insertStudent){
    students.add(insertStudent);
}
public void getStudents(){
    for(Student student: students){
        System.out.println(student.getName());
    }
}

}

I understand this works fine... the student fills in their details... but what if there are 1000 or unknown number of students?? 我知道这很好用......学生填写他们的详细信息......但是,如果有1000个或未知数量的学生怎么办? how do we get to create new instances instead of manually creating in the main "Student student2 = new Student();" 我们如何创建新实例而不是在主“Student student2 = new Student();”中手动创建 and so on...and also how do we get to add the instance of a Student into the instance of a Major that they belong to? 等等......还有我们如何将学生的实例添加到他们所属的Major的实例中?

Thank you very much! 非常感谢你!

You can simply put the majority of your main method in a while(true) loop, which will loop forever until the user inputs some sentinel value ("Enter 0 to quit"). 您可以简单地将main方法的大部分置于while(true)循环中,该循环将永远循环,直到用户输入一些sentinel值(“Enter 0 to quit”)。 When they enter "0," break out of the loop: 当他们输入“0”时,突破循环:

if(scan.nextLine().equals("0")){ break; }

Hope that helps! 希望有所帮助!

Just use a while loop 只需使用while循环

Scanner scan = new Scanner(System.in);
while(true)
{
        Student student = new Student();
        System.out.println("Enter name: ");
        student.setName(scan.next());
        System.out.println("Enter major: ");
        student.setMajor(scan.next());
        System.out.println("Do you wish to continue [Y/N] ?");
        if(scan.next().equalsIgnoreCase("N"))
        {
            break;
        }   
}

student1添加到ArrayList ,您可以删除它的引用,为其提供新的Student对象值。

First of when you set the Major, use the major1 variable instead and make the major attribute of the class Student a "Major" 首先设置Major,使用major1变量,并使Student类的主要属性为“Major”

Begin with the declaration of the Major 从专业宣言开始

Use a loop to fill everything 使用循环来填充所有内容

Major major1 = new Major();

while(true) {
    Student temp = new Student();
    System.out.println("please enter your name...");
    temp.setName(scan.next());
    System.out.println("Please enter the name of your major.");
    Major majTemp = null;
    while(majTemp == null) majTemp = getMajorPerName(scan.next());
    temp.setMajor(majTemp);
    majTemp.addStudent(temp.getName());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM