简体   繁体   English

如何在Java中访问数组并将数据分配给每个对象?

[英]How to access an array and assign data to each object in Java?

Problem: 问题:

I'm trying to figure out how to access the Student Array class in order to create four entries for each Student object, but I'm not sure how to do so, while also allowing the program to create more than just one Student . 我试图弄清楚如何访问Student Array类,以便为每个Student对象创建四个条目,但是我不确定该如何做,同时还允许该程序创建一个以上的Student

public class ClassRoster<T> {

    public static void main(String[]args) {
        ClassRoster<Student> classroster = new ClassRoster<Student>();
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Add/Drop/Search?");
        String action = keyboard.nextLine();
        boolean done = false;
        Object temp, c, d, e;
        int fresh, soph, jun, sen;
        Student test = new Student();

        while(!done) {

            if(action.equalsIgnoreCase("Add")) 
                {
                    int counter = 0;
                    System.out.print("Enter Student ID");
                    temp = test.setID(keyboard.nextInt());
                    System.out.println("First name?");
                    c = test.setFirstName(keyboard.nextLine());
                    System.out.println("Last name?");
                    d = test.setLastName(keyboard.nextLine());
                    System.out.println("Academic Level?");
                    e = test.setLevel(keyboard.nextLine());
                    ...
}

And I have another class called Student , where there are four different data entries (ID, FirstName, LastName, Academic Level). 我还有另一个班级Student ,其中有四个不同的数据条目(ID,名字,姓氏,学术水平)。

I'm not sure how to access the object which I have created in the correct way. 我不确定如何访问以正确方式创建的对象。 It just gives me an error in this Driver class, and I don't know how to correctly access the array bag. 它只是给我这个Driver类错误,我不知道如何正确访问数组包。

but I'm not sure how to do so while also allowing the program to create more than just one Student 但我不确定该怎么做,同时还允许该程序创建多个学生

Currently you are only creating one specific instance of student with Student test = new Student(); 目前,您仅使用Student test = new Student();创建一个学生的特定实例Student test = new Student(); To actually create more than one student, you will have to iterate the whole process of reading all four data entries (ID, FirstName, LastName, Academic Level). 要实际创建一个以上的学生,您将必须遍历读取所有四个数据条目(ID,名字,姓氏,学术水平)的整个过程。 Instead of having to initialize the fields (your four data entries) with specific set methods, I would recommend you letting the Student class initialize them with the class constructor. 我建议您不必让Student类使用类构造函数对其进行初始化,而不必使用特定的set方法来初始化字段(您的四个数据条目)。 Meaning the Student class should look something like this: 意味着Student类应该看起来像这样:

public class Student{
    private final int ID;
    private final String firstname;
    private final String lastname;
    private String level;

public Student(int ID, String firstname, String lastname, String level){
    this.ID = ID;
    this.firstname = firstname;
    this.lastname = lastname;
    this.level = level;
}

ID, firstname and lastname are set to final as you foresee them not to change. ID,名和姓将设置为最终名称,因为您预计它们不会更改。 However the academic level is ought to change and therefore is not set to final. 但是,学术水平应该改变,因此不能设置为最终水平。 Now that you have set up a constructor for your Student class, we can get to how to allow the program to insert multiple students at once. 现在,您已经为学生类设置了一个构造函数,我们现在可以了解如何允许该程序一次插入多个学生。

public static void main(String[]args) {
    ClassRoster<Student> classroster = new ClassRoster<Student>();
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Add/Drop/Search?");
    String action = keyboard.nextLine();
    boolean done = false;

while(!done) {

    if(action.equalsIgnoreCase("Add")) {

            System.out.print("Enter Student ID");
            int ID = keyboard.nextInt();
            System.out.println("First name?");
            String firstname = keyboard.nextLine();
            System.out.println("Last name?");
            String lastname = keyboard.nextLine();
            System.out.println("Academic Level?");
            String level = keyboard.nextLine();
            Student student = new Student(ID, firstname, lastname, level);

            //we have now created a new instance of Student, now we have to save it in your classroster

            classroster.add(student);
    }
    System.out.println("Next action?");
    action = keyboard.nextLine();
    if(action.equals("done") done = true; //if you write 'done', your loop will finish executing
}

I don't know about your implementation of classroster, but I assume you have implemented it with some kind of list or map, which is why I call the add(Student s) method after creating an instance of Student. 我不知道您如何实现classroster,但我认为您已经使用某种列表或映射实现了它,这就是为什么在创建Student实例之后调用add(Student s)方法的原因。 To actually then access all students, you will have to implement a method in classroster that returns the saved list of classroster and then iterate through the returned list in the main loop. 要实际访问所有学生,您将必须在classroster中实现一个方法,该方法返回已保存的classroster列表,然后在主循环中遍历返回的列表。 To actually see what the students look like, you will also have to implement methods for the student instances to for example print out their full names. 要实际查看学生的样子,您还必须实现学生实例的方法,例如打印出他们的全名。


I see that you are having a little trouble with arrays, maps and lists as you don't know how to access your students yet. 我发现您在使用数组,地图和列表时遇到了一些麻烦,因为您尚不知道如何访问学生。 I recommend you reading up on the difference between these three data structure types and simply try to implement them in a small example to see how they work. 我建议您阅读这三种数据结构类型之间的区别,并尝试在一个小示例中尝试实现它们,以了解它们如何工作。

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

相关问题 如何访问第一列数据并分配给 Java 数组中的另一个变量? - How to access the first column data and assign to another vaiarble in Java Array? 如何从对象数组中的1个对象访问数据-Java - How to access data from 1 object in an array of objects - java 在Java中为数组对象赋值 - Assign value to an array object in Java 无法将数组列表分配给对象的数组(Java) - unable to assign array list to an array of an Object (Java) 如何访问Java中用作对象属性的数组? - How to access an array used as an object attribute in Java? Java 访问 object 中的数组 - Java Access an Array in an object 如何从文件中的数据创建对象并将其分配给数组? - How to create an object from data in a file and assign it to an array? 如何从文件读取数据并创建对象并将其分配给数组? - How to read data from a file and create an object and assign it to an array? 如何在 Java 中编写一个循环,该循环使用存储在另一个二维数组中的数据访问二维数组,该二维数组包含每个 position 的相邻位置 - How to code a loop in Java that access a 2D array using the the data stored in another 2D array that contains the adyacent positions of each position 如何为数组中的对象分配数字? - How to assign a number to an object in an array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM