I have two objects.
Child.java
public class Child {
Parents parents;
}
Parents.java
public class Parents {
ArrayList<Child> children = new ArrayList<Child>();
}
I want them to have each other. For example:
Foo.java
public class Foo {
public static void main(String[] args) {
Child child1 = new Child();
Child child2 = new Child();
ArrayList<Child> children_list= new ArrayList<Child>();
children_list.add(child1).add(child2);
Parents parents = new Parents();
for (Child child : children_list) {
// ...
// Bind them together
// ...
}
child1.parents.equals(parents); // true
child2.parents.equals(parents); // true
// And parents.children is a list containing child1 and child2
}
}
However after much thinking, I came to a problem that they cannot seem to have each other at the same time. One of the two children will have an older parent.
parents.children.add(child);
child.parents = parents;
parents.children.set(parents.children.size() - 1, child);
This will cause the child2.parent.children
to not have child1
.