简体   繁体   中英

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. The data is Student's name and the names of his/her 4 courses. But I have to put this in a loop for the user to input at least 3 records. 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.

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.

Move your creation of the Business object into the for loop:

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.

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! Business current = (Business) businesses[i];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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