How do I establish a link/relationship between objects?

Viewed 205
public class MainClass {
    ArrayList<Man> mans = new ArrayList<Man>();
    // I'm filling in this arraylist, but I'm not writing those parts because I want to summarize the 
    //code. 
}

public class Man {
    int index_father, index_son;
    String name;
    public Man(String name){
        this.name = name;
    }
}

Some guys in this community of men have a father-son relationship. My question is that:
I'm looking for a neater way to build this father-son relationship.

I tried to establish this relationship with integers (example: which_father shows the directory where the man's father is in the 'mans' list) but this has some disadvantages:
1-When creating objects, I always have to create sons after fathers. because:

private void createMan() {
    Man man = new Man("Ethan");
    man.index_father = 4;
    mans.add(man);
    mans.get(4).index_son = mans.indexOf(man);
}

If we haven't created the father yet, the 4th element of mans will not have been created yet.
2- In cases where I delete an element from the mans arraylist, which_fathers will not show the correct father man.

I may not know enough about Java, but there must be an easy way to establish a connection between objects (like the father-son relationship in this example), right?

2 Answers

You can add a List<Man> to represent sons in Man class:

public class Man {
    String name;
    List<Man> sons = new ArrayList<>();
    
    public Man(String name){
        this.name = name;
    }
}

EDIT:

I think OOP can help to simplify things:

public class Person {
    Sex sex;
    String name;
    List<Person> children = new ArrayList<>();
    Person partner; // husband/wife

    public Person(String name, Sex sex){
        this.name = name;
        this.sex = sex;
    }
    
    public void marryTo(Person person){
        this.partner = person;
        person.setPartner(person);
    }

    public void setChildFromMarriage(Person child){
        children.add(child);
        partner.getChildren().add(child);
    }

    public enum Sex {
        MAN, WOMAN
    }
}

You can keep a List<Person>.

Usage:

List<Person> people = Arrays.asList(
        new Person("Jordan", Person.Sex.MAN),
        new Person("Tim", Person.Sex.MAN),
        new Person("Sarah", Person.Sex.WOMAN)
);

// Let's marry Jordan to Sarah
Person jordan = getPersonByName(people, "Jordan");
Person sarah = getPersonByName(people, "Sarah");
jordan.marryTo(sarah);

Person child = new Person("Jordan Jr", Person.Sex.MAN);
// We can add the child to people list if we want
jordan.setChildFromMarriage(child);

// Let's change their child name using Sarah's reference. 
getPersonByName(sarah.getChildren(), "Jordan Jr").setName("Bob");

System.out.println(child.getName()); // Bob

private static Person getPersonByName(List<Person> people, String name) {
    return people.stream()
            .filter(person -> person.getName().equals(name))
            .findFirst()
            .get();
}

Output:

Bob

So what I have to do is use references instead of integers.
I thought that the objects we created were not references, but the object itself. I thought that when we assign an object to an object, the object on the left takes all the properties of the object on the right and we have two independent objects. But what actually happens is that the object reference on the left takes the object reference on the right, and we have two references and one object.
So the only way to actually create an object is to use new Constructor().

I changed the code to:

public class Person {
    String Name;
    Gender gender;
    Person father;
    Person mother;
    Person partner;
    ArrayList<Person> childs = new ArrayList<Person>();
    
    public enum Gender {
        MAN, WOMAN
    }
    
    public Person(String name, Gender gender){
        this.Name = name;
        this.gender = gender;
    }
    
    public void marryTo(Person person){
        this.partner = person;
        person.partner = this;
    }
    
     public void setChildFromMarriage(Person child){
         childs.add(child);
         partner.childs.add(child);
     }
}

.......................................................................................... ..........................................................................................

public class MainClass {
    public static void main (String[] args)
    {
        try {
            MainClass obj = new MainClass ();
            obj.run ();
        } catch (Exception e) {
            e.printStackTrace ();
        }
    }
    
    ArrayList<Person> persons = new ArrayList<Person>();
    
    public void run () {
        Person p; 
        
        p = new Person("Jordan", Person.Gender.MAN);
        persons.add(p);
        p = new Person("Sarah", Person.Gender.WOMAN);
        persons.add(p);
        
        persons.get(0).marryTo(persons.get(1));
        
        p = new Person("Jordan Jr", Person.Gender.MAN);
        persons.add(p);
        persons.get(0).setChildFromMarriage(persons.get(2));
        
        persons.get(1).childs.get(0).Name = "Bob";
        
        System.out.println(persons.get(2).Name);
    }
}

Output:

Bob
Related