简体   繁体   English

如何在Java中将对象保留在对象数组中

[英]How to retain an object in an array of objects in Java

I'm not sure how to ask this question. 我不确定如何问这个问题。 I have to write a program that has 2 classes: one store the data and one call to display the data. 我必须编写一个包含2个类的程序:一个存储数据,另一个调用显示数据。 The data is Student's name and the names of his/her 4 courses. 数据是学生的姓名和他/她的4门课程的名称。 But I have to put this in a loop for the user to input at least 3 records. 但是我必须将此循环放置,以便用户至少输入3条记录。 If the user doesn't enter a student's name (or name = blank) get out of the loop and display the info entered. 如果用户未输入学生的姓名(或姓名=空白),则退出循环并显示输入的信息。

Example: 例:

John Doe MATH 101 ENGL 101 READ 101 COMP 101
Jane Doe PHYS 101 CHEM 101 PSYC 101 ACCT 101
Mary Doe PHED 101 HIST 101 CALC 101 POLS 101

What I'm trying to do is make each of the students' record an object and store those 3 objects in an array of objects then display it. 我想做的是让每个学生记录一个对象,并将这3个对象存储在对象数组中,然后显示它。

Below is my code: 下面是我的代码:

import java.util.Scanner;
public class UserInterface {
    public static void main(String[] args) {
    //Create a scanner
    Scanner input = new Scanner(System.in);

    //Create an object from Business class
    Business b = new Business();

    //Declare variables
    final int NUMBER_OF_COURSES = 4;
    String[] coursesName = new String[4];
    Business[] businessArray = new Business[3]; //Declare a array of objects

    for (int counter = 0; counter < businessArray.length; counter++) {

    //Prompt user to input name
    System.out.println("Enter student's name: ");
    b.setName(input.nextLine());

    for (int i = 0; i < NUMBER_OF_COURSES; i++) {
    System.out.println("Enter " + b.getName() + "'s course number " + (i + 1));
    coursesName[i] = input.nextLine();
    }//end of for(i)-loop

    b.setCourses(coursesName);

    businessArray[counter] = b;

    System.out.println(businessArray[counter]); //Here it display correctly for each round

    }//End of for(counter)-loop

    for (int pa = 0; pa < businessArray.length; pa++)
        System.out.println(businessArray[pa]);         //but here it displays 3 records of the last entry
                                                        //so my question is how do I retain each entry in its own object and 
                                                     //adds it to the array of objects? 
                                                     //I know that b gets overwrite by the last data entered because
                                                    //it is just a pointer to that object.  

    input.close();

    }//End of main method

}//End of class UserInterface


The other class:

public class Business {

    //Declare variables
    private String name;
    private String[] courses = new String[4];

    //Default Constructor
    public Business(){

    }

    //getter for student's name
    public String getName() {
        return name;
    }

    //setter for student's name
    public void setName(String name) {
        this.name = name;
    }

    //getter for courses' name
    public String[] getCourses() {
        return courses;
    }

    //setter for courses' name
    public void setCourses(String[] courses) {
        this.courses = courses;
    }

}//End of class Business

I know my codes are not good. 我知道我的代码不好。 But I'm required to have getters and setters for each variable in this Business class. 但是我需要为此业务类中的每个变量都具有getter和setter方法。

Move your creation of the Business object into the for loop: 将您创建的Business对象移到for循环中:

for (int counter = 0; counter < businessArray.length; counter++) {
    Business b = new Business();
    // ...
}

Right now, every entry in the array points to the same object, so you're overwriting the values in it repeatedly. 现在,数组中的每个条目都指向同一个对象,因此您要重复覆盖其中的值。 Moving the creation into the loop means you'll have a different object for each slot of the array. 将创建的内容移入循环意味着对数组的每个插槽都有一个不同的对象。

String retainCourse(int pointer){
    return this.courses[pointer];
}

Add this function to your business class. 将此功能添加到您的业务类别。

You should overwrite the .toString() method of the class to get the expected result. 您应该覆盖该类的.toString()方法以获得预期的结果。

In a for loop it is a better practice to get the current object and set it as a temporary variable casted to the exact class! 在for循环中,更好的做法是获取当前对象并将其设置为强制转换为确切类的临时变量! Business current = (Business) businesses[i];

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

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