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

时间:2018-10-25 04:27:08

标签: java arrays class object

我不确定如何问这个问题。我必须编写一个包含2个类的程序:一个存储数据,另一个调用显示数据。数据是学生的姓名和他/她的4门课程的名称。但是我必须将此循环放置,以便用户至少输入3条记录。如果用户未输入学生的姓名(或姓名=空白),则退出循环并显示输入的信息。

示例:

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

我要做的是让每个学生记录一个对象,并将这三个对象存储在对象数组中,然后显示它。

下面是我的代码:

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

我知道我的代码不好。但是我需要在此Business类中为每个变量都具有getter和setter。

2 个答案:

答案 0 :(得分:1)

将创建的Business对象移到for循环中:

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

现在,数组中的每个条目都指向同一个对象,因此您要重复覆盖其中的值。将创建内容移入循环意味着您将为数组的每个插槽拥有一个不同的对象。

答案 1 :(得分:-1)

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

将此功能添加到您的业务类别中。

您应该覆盖该类的.toString()方法以获得预期的结果。

在for循环中,更好的做法是获取当前对象并将其设置为强制转换为确切类的临时变量! Business current = (Business) businesses[i];