An object, within an object, within an array of objects, within an array of objects in Java?












-2















EDIT:
I see I worded my original question incorrectly, there should have been 2 arrays, I have amended that to be more clear.
I've got everything working now EXCEPT the getter method for the departments and the locations. I have tried numerous ways to display this, but it will only show the address no matter what I try. This is what I have (location is the same, just in a different object with different names):



public void getDepartments () {
for(int i = 0; i < departments.size(); i++) {
System.out.println(departments.get(i));
}
}


This works for the Employees arraylist, but not the departments or locations arraylist.



Working on this project and I honestly have no idea what to do. Essentially breaks down like this:



Create object1 with 2 variables, (lets say this is person, with name and age as variables).



Create object2 as a subclass of object1 with 2 additional variables, (lets call this one employee, with variables employee ID and position)



Create object3 with a name variable and an array of object2, (so name is department, and the array would hold all the employees that work there)



Create object4 which holds an array of object3. (this is an array of office locations)



Essentially I should be able to pull a list showing the office location, with a list of departments at that location, and a list of employees in those departments.



Heres what I have coded:



Object 1:



public class Person {

private String name;
private int age;

public Person () {
name = "";
age = 0;
}

public Person (String name, int age) {
this.name = name;
this.age = age;
}

public void setName (String name) {
this.name = name;
}

public String getName () {
return name;
}

public void setAge (int age) {
this.age = age;
}

public int getAge () {
return age;
}

public String toString () {
return ("Name: " + name + ", Age: " + age);
}
}


Object 2:



public class Employee extends Person {

private String employeeID;
private String position;

public Employee (){
super ();
employeeID = "";
position = "";
}

public Student (String name, int age, String employeeID, String position){
super (name, age);
this.employeeID = employeeID;
this.position = position;
}

public void setEmployeeID (String employeeID) {
this.employeeID = employeeID;
}

public String getEmployeeID () {
return employeeID;
}

public void setPosition (String position) {
this.year = position;
}

public String getPosition() {
return year;
}

public String toString () {
return (super.toString() + ", Employee ID: " + employeeID + ", Position:
" + position);
}

}


Object 3: (obviously a lot missing at this point)



public class Department {

private String departmentName;
private int numberOfEmployees;
private Employee employees;

public Department() {

}

public Department (String departmentName, int numberOfEmployees) {
this.departmentName = departmentName;
this.numberOfEmployees = numberOfEmployees;
Employee employees = new Employee[numberOfEmployees];
}

public String getName() {
return departmentName;
}

public void setName(String departmentName) {
this.departmentName = departmentName;
}

public int getNumberOfEmployees() {
return numberOfEmployees;
}

public void setNumberOfEmployees(int numberOfEmployees) {
this.numberOfEmployees = numberOfEmployees;
}

}









share|improve this question




















  • 3





    Please read about Java Inheritance first

    – Andreas
    Nov 21 '18 at 4:22











  • Do have some basic understanding of OOPS principle first & then try out the same problem on your own. If it doesn’t work out then post your question with your query.

    – Abhinav
    Nov 21 '18 at 4:26











  • It would be best to post some code which shows your work. For example, you say you have no idea how these arrays work, but can you make any kind of array? Like an array of String?

    – markspace
    Nov 21 '18 at 4:27











  • It's actually interesting to know that you have found an example to describe that but you have no idea what to do. Perhaps what you need is collections like List (i.e. ArrayList).

    – Jai
    Nov 21 '18 at 4:33











  • Re: the code you added: so, just make an array of Department, is that hard? What exactly are you having trouble with?

    – markspace
    Nov 21 '18 at 5:58


















-2















EDIT:
I see I worded my original question incorrectly, there should have been 2 arrays, I have amended that to be more clear.
I've got everything working now EXCEPT the getter method for the departments and the locations. I have tried numerous ways to display this, but it will only show the address no matter what I try. This is what I have (location is the same, just in a different object with different names):



public void getDepartments () {
for(int i = 0; i < departments.size(); i++) {
System.out.println(departments.get(i));
}
}


This works for the Employees arraylist, but not the departments or locations arraylist.



Working on this project and I honestly have no idea what to do. Essentially breaks down like this:



Create object1 with 2 variables, (lets say this is person, with name and age as variables).



Create object2 as a subclass of object1 with 2 additional variables, (lets call this one employee, with variables employee ID and position)



Create object3 with a name variable and an array of object2, (so name is department, and the array would hold all the employees that work there)



Create object4 which holds an array of object3. (this is an array of office locations)



Essentially I should be able to pull a list showing the office location, with a list of departments at that location, and a list of employees in those departments.



Heres what I have coded:



Object 1:



public class Person {

private String name;
private int age;

public Person () {
name = "";
age = 0;
}

public Person (String name, int age) {
this.name = name;
this.age = age;
}

public void setName (String name) {
this.name = name;
}

public String getName () {
return name;
}

public void setAge (int age) {
this.age = age;
}

public int getAge () {
return age;
}

public String toString () {
return ("Name: " + name + ", Age: " + age);
}
}


Object 2:



public class Employee extends Person {

private String employeeID;
private String position;

public Employee (){
super ();
employeeID = "";
position = "";
}

public Student (String name, int age, String employeeID, String position){
super (name, age);
this.employeeID = employeeID;
this.position = position;
}

public void setEmployeeID (String employeeID) {
this.employeeID = employeeID;
}

public String getEmployeeID () {
return employeeID;
}

public void setPosition (String position) {
this.year = position;
}

public String getPosition() {
return year;
}

public String toString () {
return (super.toString() + ", Employee ID: " + employeeID + ", Position:
" + position);
}

}


Object 3: (obviously a lot missing at this point)



public class Department {

private String departmentName;
private int numberOfEmployees;
private Employee employees;

public Department() {

}

public Department (String departmentName, int numberOfEmployees) {
this.departmentName = departmentName;
this.numberOfEmployees = numberOfEmployees;
Employee employees = new Employee[numberOfEmployees];
}

public String getName() {
return departmentName;
}

public void setName(String departmentName) {
this.departmentName = departmentName;
}

public int getNumberOfEmployees() {
return numberOfEmployees;
}

public void setNumberOfEmployees(int numberOfEmployees) {
this.numberOfEmployees = numberOfEmployees;
}

}









share|improve this question




















  • 3





    Please read about Java Inheritance first

    – Andreas
    Nov 21 '18 at 4:22











  • Do have some basic understanding of OOPS principle first & then try out the same problem on your own. If it doesn’t work out then post your question with your query.

    – Abhinav
    Nov 21 '18 at 4:26











  • It would be best to post some code which shows your work. For example, you say you have no idea how these arrays work, but can you make any kind of array? Like an array of String?

    – markspace
    Nov 21 '18 at 4:27











  • It's actually interesting to know that you have found an example to describe that but you have no idea what to do. Perhaps what you need is collections like List (i.e. ArrayList).

    – Jai
    Nov 21 '18 at 4:33











  • Re: the code you added: so, just make an array of Department, is that hard? What exactly are you having trouble with?

    – markspace
    Nov 21 '18 at 5:58
















-2












-2








-2








EDIT:
I see I worded my original question incorrectly, there should have been 2 arrays, I have amended that to be more clear.
I've got everything working now EXCEPT the getter method for the departments and the locations. I have tried numerous ways to display this, but it will only show the address no matter what I try. This is what I have (location is the same, just in a different object with different names):



public void getDepartments () {
for(int i = 0; i < departments.size(); i++) {
System.out.println(departments.get(i));
}
}


This works for the Employees arraylist, but not the departments or locations arraylist.



Working on this project and I honestly have no idea what to do. Essentially breaks down like this:



Create object1 with 2 variables, (lets say this is person, with name and age as variables).



Create object2 as a subclass of object1 with 2 additional variables, (lets call this one employee, with variables employee ID and position)



Create object3 with a name variable and an array of object2, (so name is department, and the array would hold all the employees that work there)



Create object4 which holds an array of object3. (this is an array of office locations)



Essentially I should be able to pull a list showing the office location, with a list of departments at that location, and a list of employees in those departments.



Heres what I have coded:



Object 1:



public class Person {

private String name;
private int age;

public Person () {
name = "";
age = 0;
}

public Person (String name, int age) {
this.name = name;
this.age = age;
}

public void setName (String name) {
this.name = name;
}

public String getName () {
return name;
}

public void setAge (int age) {
this.age = age;
}

public int getAge () {
return age;
}

public String toString () {
return ("Name: " + name + ", Age: " + age);
}
}


Object 2:



public class Employee extends Person {

private String employeeID;
private String position;

public Employee (){
super ();
employeeID = "";
position = "";
}

public Student (String name, int age, String employeeID, String position){
super (name, age);
this.employeeID = employeeID;
this.position = position;
}

public void setEmployeeID (String employeeID) {
this.employeeID = employeeID;
}

public String getEmployeeID () {
return employeeID;
}

public void setPosition (String position) {
this.year = position;
}

public String getPosition() {
return year;
}

public String toString () {
return (super.toString() + ", Employee ID: " + employeeID + ", Position:
" + position);
}

}


Object 3: (obviously a lot missing at this point)



public class Department {

private String departmentName;
private int numberOfEmployees;
private Employee employees;

public Department() {

}

public Department (String departmentName, int numberOfEmployees) {
this.departmentName = departmentName;
this.numberOfEmployees = numberOfEmployees;
Employee employees = new Employee[numberOfEmployees];
}

public String getName() {
return departmentName;
}

public void setName(String departmentName) {
this.departmentName = departmentName;
}

public int getNumberOfEmployees() {
return numberOfEmployees;
}

public void setNumberOfEmployees(int numberOfEmployees) {
this.numberOfEmployees = numberOfEmployees;
}

}









share|improve this question
















EDIT:
I see I worded my original question incorrectly, there should have been 2 arrays, I have amended that to be more clear.
I've got everything working now EXCEPT the getter method for the departments and the locations. I have tried numerous ways to display this, but it will only show the address no matter what I try. This is what I have (location is the same, just in a different object with different names):



public void getDepartments () {
for(int i = 0; i < departments.size(); i++) {
System.out.println(departments.get(i));
}
}


This works for the Employees arraylist, but not the departments or locations arraylist.



Working on this project and I honestly have no idea what to do. Essentially breaks down like this:



Create object1 with 2 variables, (lets say this is person, with name and age as variables).



Create object2 as a subclass of object1 with 2 additional variables, (lets call this one employee, with variables employee ID and position)



Create object3 with a name variable and an array of object2, (so name is department, and the array would hold all the employees that work there)



Create object4 which holds an array of object3. (this is an array of office locations)



Essentially I should be able to pull a list showing the office location, with a list of departments at that location, and a list of employees in those departments.



Heres what I have coded:



Object 1:



public class Person {

private String name;
private int age;

public Person () {
name = "";
age = 0;
}

public Person (String name, int age) {
this.name = name;
this.age = age;
}

public void setName (String name) {
this.name = name;
}

public String getName () {
return name;
}

public void setAge (int age) {
this.age = age;
}

public int getAge () {
return age;
}

public String toString () {
return ("Name: " + name + ", Age: " + age);
}
}


Object 2:



public class Employee extends Person {

private String employeeID;
private String position;

public Employee (){
super ();
employeeID = "";
position = "";
}

public Student (String name, int age, String employeeID, String position){
super (name, age);
this.employeeID = employeeID;
this.position = position;
}

public void setEmployeeID (String employeeID) {
this.employeeID = employeeID;
}

public String getEmployeeID () {
return employeeID;
}

public void setPosition (String position) {
this.year = position;
}

public String getPosition() {
return year;
}

public String toString () {
return (super.toString() + ", Employee ID: " + employeeID + ", Position:
" + position);
}

}


Object 3: (obviously a lot missing at this point)



public class Department {

private String departmentName;
private int numberOfEmployees;
private Employee employees;

public Department() {

}

public Department (String departmentName, int numberOfEmployees) {
this.departmentName = departmentName;
this.numberOfEmployees = numberOfEmployees;
Employee employees = new Employee[numberOfEmployees];
}

public String getName() {
return departmentName;
}

public void setName(String departmentName) {
this.departmentName = departmentName;
}

public int getNumberOfEmployees() {
return numberOfEmployees;
}

public void setNumberOfEmployees(int numberOfEmployees) {
this.numberOfEmployees = numberOfEmployees;
}

}






java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 18:05







TheGrinch

















asked Nov 21 '18 at 4:19









TheGrinchTheGrinch

23




23








  • 3





    Please read about Java Inheritance first

    – Andreas
    Nov 21 '18 at 4:22











  • Do have some basic understanding of OOPS principle first & then try out the same problem on your own. If it doesn’t work out then post your question with your query.

    – Abhinav
    Nov 21 '18 at 4:26











  • It would be best to post some code which shows your work. For example, you say you have no idea how these arrays work, but can you make any kind of array? Like an array of String?

    – markspace
    Nov 21 '18 at 4:27











  • It's actually interesting to know that you have found an example to describe that but you have no idea what to do. Perhaps what you need is collections like List (i.e. ArrayList).

    – Jai
    Nov 21 '18 at 4:33











  • Re: the code you added: so, just make an array of Department, is that hard? What exactly are you having trouble with?

    – markspace
    Nov 21 '18 at 5:58
















  • 3





    Please read about Java Inheritance first

    – Andreas
    Nov 21 '18 at 4:22











  • Do have some basic understanding of OOPS principle first & then try out the same problem on your own. If it doesn’t work out then post your question with your query.

    – Abhinav
    Nov 21 '18 at 4:26











  • It would be best to post some code which shows your work. For example, you say you have no idea how these arrays work, but can you make any kind of array? Like an array of String?

    – markspace
    Nov 21 '18 at 4:27











  • It's actually interesting to know that you have found an example to describe that but you have no idea what to do. Perhaps what you need is collections like List (i.e. ArrayList).

    – Jai
    Nov 21 '18 at 4:33











  • Re: the code you added: so, just make an array of Department, is that hard? What exactly are you having trouble with?

    – markspace
    Nov 21 '18 at 5:58










3




3





Please read about Java Inheritance first

– Andreas
Nov 21 '18 at 4:22





Please read about Java Inheritance first

– Andreas
Nov 21 '18 at 4:22













Do have some basic understanding of OOPS principle first & then try out the same problem on your own. If it doesn’t work out then post your question with your query.

– Abhinav
Nov 21 '18 at 4:26





Do have some basic understanding of OOPS principle first & then try out the same problem on your own. If it doesn’t work out then post your question with your query.

– Abhinav
Nov 21 '18 at 4:26













It would be best to post some code which shows your work. For example, you say you have no idea how these arrays work, but can you make any kind of array? Like an array of String?

– markspace
Nov 21 '18 at 4:27





It would be best to post some code which shows your work. For example, you say you have no idea how these arrays work, but can you make any kind of array? Like an array of String?

– markspace
Nov 21 '18 at 4:27













It's actually interesting to know that you have found an example to describe that but you have no idea what to do. Perhaps what you need is collections like List (i.e. ArrayList).

– Jai
Nov 21 '18 at 4:33





It's actually interesting to know that you have found an example to describe that but you have no idea what to do. Perhaps what you need is collections like List (i.e. ArrayList).

– Jai
Nov 21 '18 at 4:33













Re: the code you added: so, just make an array of Department, is that hard? What exactly are you having trouble with?

– markspace
Nov 21 '18 at 5:58







Re: the code you added: so, just make an array of Department, is that hard? What exactly are you having trouble with?

– markspace
Nov 21 '18 at 5:58














1 Answer
1






active

oldest

votes


















0














You should read carefully OOP principles. It is fundamental but really important for your logic thought.



I just help you to clarify your question.
You need to define a Person class like (for object1)



public abstract class Person {
protected String name;
protected Integer age;
public Person(String n, Integer a){
name = n;
age = a;
}
@Override
public String toString() {
//print out person's info
}
//TODO: you should create getter/setter by yourself.
}


Next will be Employee class which is subclass of Person:



public class Employee extends Person {
private String employeeId;
private String position;
public Employee (String name, int age, String id, String pos) {
//parent constructor
super(name, age);
employeeId = id;
position = pos;
}
@Override
public String toString() {
//print out employee's info
}
//TODO: you should create getter/setter by yourself.
}


Third will be Office:



public class Office {
private String location;
//you can use Person here because it is parent of Employee
private List<Person> employees;
//TODO: you should create getter/setter by yourself.
public Office(String loc) {
location = loc;
employees = new ArrayList<Person>();
}
public void addEmployee(Person p) {
employees.add(p);
}
}


Then in your main class, you can declare objects like this:



   Person p1 = new Employee("Mr.X", 30, "ID001", "Manager");
Person p2 = new Employee("Mr.Y", 31, "ID002", "Director");

Office office = new Office("Silicon Valley");
office.addEmployee(p1);
office.addEmployee(p2);

List<Office> offices =new ArrayList<Office>();
offices.add(office);





share|improve this answer
























  • This is helpful, thank you. But I'm still confused because location and Office also need to be arrays of objects. I need to be able to declare multiple locations in main, with an array of object Office within it, and an array of object Employee within that. Not sure how that is accomplished.

    – TheGrinch
    Nov 21 '18 at 15:53











  • I think maybe Ive got it....I'll keep plugging away.

    – TheGrinch
    Nov 21 '18 at 16:24











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405194%2fan-object-within-an-object-within-an-array-of-objects-within-an-array-of-obje%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You should read carefully OOP principles. It is fundamental but really important for your logic thought.



I just help you to clarify your question.
You need to define a Person class like (for object1)



public abstract class Person {
protected String name;
protected Integer age;
public Person(String n, Integer a){
name = n;
age = a;
}
@Override
public String toString() {
//print out person's info
}
//TODO: you should create getter/setter by yourself.
}


Next will be Employee class which is subclass of Person:



public class Employee extends Person {
private String employeeId;
private String position;
public Employee (String name, int age, String id, String pos) {
//parent constructor
super(name, age);
employeeId = id;
position = pos;
}
@Override
public String toString() {
//print out employee's info
}
//TODO: you should create getter/setter by yourself.
}


Third will be Office:



public class Office {
private String location;
//you can use Person here because it is parent of Employee
private List<Person> employees;
//TODO: you should create getter/setter by yourself.
public Office(String loc) {
location = loc;
employees = new ArrayList<Person>();
}
public void addEmployee(Person p) {
employees.add(p);
}
}


Then in your main class, you can declare objects like this:



   Person p1 = new Employee("Mr.X", 30, "ID001", "Manager");
Person p2 = new Employee("Mr.Y", 31, "ID002", "Director");

Office office = new Office("Silicon Valley");
office.addEmployee(p1);
office.addEmployee(p2);

List<Office> offices =new ArrayList<Office>();
offices.add(office);





share|improve this answer
























  • This is helpful, thank you. But I'm still confused because location and Office also need to be arrays of objects. I need to be able to declare multiple locations in main, with an array of object Office within it, and an array of object Employee within that. Not sure how that is accomplished.

    – TheGrinch
    Nov 21 '18 at 15:53











  • I think maybe Ive got it....I'll keep plugging away.

    – TheGrinch
    Nov 21 '18 at 16:24
















0














You should read carefully OOP principles. It is fundamental but really important for your logic thought.



I just help you to clarify your question.
You need to define a Person class like (for object1)



public abstract class Person {
protected String name;
protected Integer age;
public Person(String n, Integer a){
name = n;
age = a;
}
@Override
public String toString() {
//print out person's info
}
//TODO: you should create getter/setter by yourself.
}


Next will be Employee class which is subclass of Person:



public class Employee extends Person {
private String employeeId;
private String position;
public Employee (String name, int age, String id, String pos) {
//parent constructor
super(name, age);
employeeId = id;
position = pos;
}
@Override
public String toString() {
//print out employee's info
}
//TODO: you should create getter/setter by yourself.
}


Third will be Office:



public class Office {
private String location;
//you can use Person here because it is parent of Employee
private List<Person> employees;
//TODO: you should create getter/setter by yourself.
public Office(String loc) {
location = loc;
employees = new ArrayList<Person>();
}
public void addEmployee(Person p) {
employees.add(p);
}
}


Then in your main class, you can declare objects like this:



   Person p1 = new Employee("Mr.X", 30, "ID001", "Manager");
Person p2 = new Employee("Mr.Y", 31, "ID002", "Director");

Office office = new Office("Silicon Valley");
office.addEmployee(p1);
office.addEmployee(p2);

List<Office> offices =new ArrayList<Office>();
offices.add(office);





share|improve this answer
























  • This is helpful, thank you. But I'm still confused because location and Office also need to be arrays of objects. I need to be able to declare multiple locations in main, with an array of object Office within it, and an array of object Employee within that. Not sure how that is accomplished.

    – TheGrinch
    Nov 21 '18 at 15:53











  • I think maybe Ive got it....I'll keep plugging away.

    – TheGrinch
    Nov 21 '18 at 16:24














0












0








0







You should read carefully OOP principles. It is fundamental but really important for your logic thought.



I just help you to clarify your question.
You need to define a Person class like (for object1)



public abstract class Person {
protected String name;
protected Integer age;
public Person(String n, Integer a){
name = n;
age = a;
}
@Override
public String toString() {
//print out person's info
}
//TODO: you should create getter/setter by yourself.
}


Next will be Employee class which is subclass of Person:



public class Employee extends Person {
private String employeeId;
private String position;
public Employee (String name, int age, String id, String pos) {
//parent constructor
super(name, age);
employeeId = id;
position = pos;
}
@Override
public String toString() {
//print out employee's info
}
//TODO: you should create getter/setter by yourself.
}


Third will be Office:



public class Office {
private String location;
//you can use Person here because it is parent of Employee
private List<Person> employees;
//TODO: you should create getter/setter by yourself.
public Office(String loc) {
location = loc;
employees = new ArrayList<Person>();
}
public void addEmployee(Person p) {
employees.add(p);
}
}


Then in your main class, you can declare objects like this:



   Person p1 = new Employee("Mr.X", 30, "ID001", "Manager");
Person p2 = new Employee("Mr.Y", 31, "ID002", "Director");

Office office = new Office("Silicon Valley");
office.addEmployee(p1);
office.addEmployee(p2);

List<Office> offices =new ArrayList<Office>();
offices.add(office);





share|improve this answer













You should read carefully OOP principles. It is fundamental but really important for your logic thought.



I just help you to clarify your question.
You need to define a Person class like (for object1)



public abstract class Person {
protected String name;
protected Integer age;
public Person(String n, Integer a){
name = n;
age = a;
}
@Override
public String toString() {
//print out person's info
}
//TODO: you should create getter/setter by yourself.
}


Next will be Employee class which is subclass of Person:



public class Employee extends Person {
private String employeeId;
private String position;
public Employee (String name, int age, String id, String pos) {
//parent constructor
super(name, age);
employeeId = id;
position = pos;
}
@Override
public String toString() {
//print out employee's info
}
//TODO: you should create getter/setter by yourself.
}


Third will be Office:



public class Office {
private String location;
//you can use Person here because it is parent of Employee
private List<Person> employees;
//TODO: you should create getter/setter by yourself.
public Office(String loc) {
location = loc;
employees = new ArrayList<Person>();
}
public void addEmployee(Person p) {
employees.add(p);
}
}


Then in your main class, you can declare objects like this:



   Person p1 = new Employee("Mr.X", 30, "ID001", "Manager");
Person p2 = new Employee("Mr.Y", 31, "ID002", "Director");

Office office = new Office("Silicon Valley");
office.addEmployee(p1);
office.addEmployee(p2);

List<Office> offices =new ArrayList<Office>();
offices.add(office);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 4:48









htpvlhtpvl

63739




63739













  • This is helpful, thank you. But I'm still confused because location and Office also need to be arrays of objects. I need to be able to declare multiple locations in main, with an array of object Office within it, and an array of object Employee within that. Not sure how that is accomplished.

    – TheGrinch
    Nov 21 '18 at 15:53











  • I think maybe Ive got it....I'll keep plugging away.

    – TheGrinch
    Nov 21 '18 at 16:24



















  • This is helpful, thank you. But I'm still confused because location and Office also need to be arrays of objects. I need to be able to declare multiple locations in main, with an array of object Office within it, and an array of object Employee within that. Not sure how that is accomplished.

    – TheGrinch
    Nov 21 '18 at 15:53











  • I think maybe Ive got it....I'll keep plugging away.

    – TheGrinch
    Nov 21 '18 at 16:24

















This is helpful, thank you. But I'm still confused because location and Office also need to be arrays of objects. I need to be able to declare multiple locations in main, with an array of object Office within it, and an array of object Employee within that. Not sure how that is accomplished.

– TheGrinch
Nov 21 '18 at 15:53





This is helpful, thank you. But I'm still confused because location and Office also need to be arrays of objects. I need to be able to declare multiple locations in main, with an array of object Office within it, and an array of object Employee within that. Not sure how that is accomplished.

– TheGrinch
Nov 21 '18 at 15:53













I think maybe Ive got it....I'll keep plugging away.

– TheGrinch
Nov 21 '18 at 16:24





I think maybe Ive got it....I'll keep plugging away.

– TheGrinch
Nov 21 '18 at 16:24


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405194%2fan-object-within-an-object-within-an-array-of-objects-within-an-array-of-obje%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

'app-layout' is not a known element: how to share Component with different Modules