Home > Back-end >  JAVA Attribute of ArrayList<Object> not returning values
JAVA Attribute of ArrayList<Object> not returning values

Time:11-12

Class Hospital

package app;

import java.util.ArrayList;
import app.Department;

public class Hospital {
    String hospitalId;
    String hospitalName;
    int departmentCount;
    ArrayList<Department> departmentList = new ArrayList<Department>();

    public Hospital(String hospitalId, String hospitalName, ArrayList<Department> departmentList, int departmentCount) {
        this.hospitalId = hospitalId;
        this.hospitalName = hospitalName;
        this.departmentList = departmentList;
        this.departmentCount = departmentCount;
    }
    
    public ArrayList<Department> getDepartmentList() {
        System.out.println("Returning "   departmentList.size());
        return departmentList;
    }

}

Class Department

package app;

public class Department {
    String departmentId;
    private String hospitalDeptName;
    String hospitalDeptType;
    String subtypeBitFlag;
    String status;

    public Department(String departmentId, String hospitalDeptName, String hospitalDeptType, String subtypeBitFlag, String status) {
        this.departmentId = departmentId;
        this.hospitalDeptName = hospitalDeptName;
        this.hospitalDeptType = hospitalDeptType;
        this.subtypeBitFlag = subtypeBitFlag;
        this.status = status;
    }
    
    public String getDepartmentName() {
        return hospitalDeptName;
    }
}

I am creating an object Department and storing them in an ArrayList

ArrayList<Department> departmentList = new ArrayList<Department>();
departmentList.add(new Department(departmentId, hospitalDeptName, hospitalDeptType, subtypeBitFlag, status));

I am creating an object Hospital and passing to the constructor the above departmentList as list of Departments

ArrayList<Hospital> hospitalList = new ArrayList<Hospital>();
Hospital hospital = new Hospital(hospitalId, hospitalName, departmentList, departmentList.size());

Looping over the hospital objects and extracting the information

            for (Hospital hospital : hospitalList) {
                System.out.println("The number of departments is: " hospital.departmentList.size()   " for Hospital: "   hospital.hospitalId );
                
                for (Department department : hospital.departmentList) {
                    System.out.println("Hospital ID: "  hospital.hospitalId   ", Hospital Name: "  hospital.hospitalName   ", Department Name: "   department.getDepartmentName());
                }
            }

Logs as seen below. I am not able to retrive the department and not even their values. But... I am able to retrieve anything other that ArrayList from the object Hospital. The count 0 proves is as if the object Hospital is not populating the array list of Department

The number of departments is: 0 for Hospital: q
The number of departments is: 0 for Hospital: a
The number of departments is: 0 for Hospital: s
The number of departments is: 0 for Hospital: 1
The number of departments is: 0 for Hospital: 2
The number of departments is: 0 for Hospital: 3

CodePudding user response:

You made just one arraylist, and all the hospitals just have a reference to the same list, which you later clear, hence, that got rid of all departments.

Let's take it one step at a time:

ArrayList<Department> departmentList = new ArrayList<Department>();

This makes a new arraylist object, assigns the reference to it to a newly declared local variable named departmentList. new ArrayList() is like "build a house" and ArrayList<Department> departmentList; is like 'open up a new page in your address book and title it "departmentList". departmentList = new... is: Write the address to that house down in the address book. Crucial take awawy: departmentList is not the same as the list - it's a reference to it - it's the page in the address book that you can use to get to the house, not the house itself.

departmentList.add(new Department(departmentId, hospitalDeptName, > hospitalDeptType, subtypeBitFlag, status));

. is the dereference operator: This is the equivalent of "Take the page in your address book titled "departmentList" and then go walk over to the house itself. Open the door and yell "ADD!" at it. Things will probably happen when you do this.

Hospital hospital = new Hospital(hospitalId, hospitalName, departmentList, departmentList.size());

Focusing on the departmentList part: This says: "Grab a piece of paper and copy the address over onto it, then hand that piece of paper to the hospital constructor". It does not clone the list.

public Hospital(String hospitalId, String hospitalName, ArrayList<Department> departmentList, int departmentCount) {
    this.departmentList = departmentList;
}

More copying of address book pages. At no point did anybody copy a house anywhere.

Now there are many notebook pages floating about, all with the same address on it. If anybody that has a page decides to visit a house and toss a brick through the window, everybody else will see that too. This isn't good - someone can change the departmentlist in your hospital now. You probably wanted to clone that arraylist instead.

Your Hospital constructor should probably have:

this.departmentList = List.copyOf(departmentList);

instead.

  • Related