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
hospitalListdoes not escape anywhere else afternew Hospitalis constructed? Can you show us the full scope (e.g. surrounding function) ofhospitalList, just to make sure that it's not touched by anything else after the creation of the hospital.hospitalList. Add the section where you are adding into this list.hospitalList.add(hospital)this.departmentList=departmentList;to:this.departmentList = (ArrayList)departmentList.clone();