0

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
5
  • 1
    And you are definitely sure that the reference to hospitalList does not escape anywhere else after new Hospital is constructed? Can you show us the full scope (e.g. surrounding function) of hospitalList, just to make sure that it's not touched by anything else after the creation of the hospital. Commented Nov 12, 2022 at 1:01
  • 1
    Please edit your question to provide a runnable example. Commented Nov 12, 2022 at 1:03
  • 1
    take a look at how you're adding hospitals to hospitalList. Add the section where you are adding into this list. Commented Nov 12, 2022 at 1:41
  • 1
    From you're current code I suppose you're missing hospitalList.add(hospital) Commented Nov 12, 2022 at 1:44
  • @GauravkumarSingh I do have it. I was able to resolve this issue by changing my constructor from this.departmentList=departmentList; to: this.departmentList = (ArrayList)departmentList.clone(); Commented Nov 12, 2022 at 3:19

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Amazing explanation with the example! That fixed it. I used this.departmentList = (ArrayList)departmentList.clone();
I'd go with List.copyOf, that way the departmentlist can't be changed by anybody. If you really want an arraylist, new ArrayList<>(departmentList); is better (it's idiomatic (other java programmers would do it that way), does the same thing, at the same speed, with fewer warnings).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.