how to add or copy an arraylist to the inner class which is an arraylist of objects?
in
Programming Questions
•
3 years ago
decided to start a new post - further to recent discussion "
problem creating an agent's copy of another objects ArrayList ".
i still can't solve this.
I understand what PhiLho is suggesting in his last response but i don't understand how to do it.
I have tried to simplify my problem in the code below.
in this sketch there are three composite objects (families; each being a mother with 3 kids. one daughter and two sons.)
each family is a friend of the others - bar one child - who deletes one friend -
only that one child should have a different friends list .
but this prints out:
1: girls friends[2, 3]
1: 0: boys friends[2, 3]
1: 1: boys friends[2, 3]
2: girls friends[1, 3]
2: 0: boys friends[1, 3]
2: 0: i've had a fight with friend 3!
2: 1: boys friends[1]
3: girls friends[1, 2]
3: 0: boys friends[1, 2]
3: 1: boys friends[1, 2]
1: 0: boys friends[2, 3]
1: 1: boys friends[2, 3]
2: girls friends[1, 3]
2: 0: boys friends[1, 3]
2: 0: i've had a fight with friend 3!
2: 1: boys friends[1]
3: girls friends[1, 2]
3: 0: boys friends[1, 2]
3: 1: boys friends[1, 2]
the highlight shows that the brother (being an element of the same arrayList 'boys') has also had this friend removed.
I understand that i have setup the classes in a way that the list is referenced to the 'boys' rather than being copied. But i cannot figure out how to do this copy or how to add the values in way other than i have; so that "if you remove a reference from one, it will still be in the other"
can anyone explain how i can do this please. thanks!
ps: this runs with the xml file data added below at end
pss: i have looked at clone() but i don't understand how to implement it.
- //sketch of family relations
- //3 families: each family is a unit being
- //a mother with 3 children: One daughter and Two sons.
- //each family is friends with the others
- //
- //OOP properties:
- //each family member is a class-object
- //Mother class is composed of children -
- //daughter and son classes are inner classes of mother class
- //sons are in an arrayList
- XMLElement xml;
- int mums = 3;
- Mother [] mum = new Mother [mums];
- void setup() {
- //load family relations from file
- xml = new XMLElement(this, "relations.xml");
- mums = xml.getChildCount();
- XMLElement family;
- XMLElement identity;
- XMLElement friend1;
- XMLElement friend2;
- for (int i=0; i<mum.length; i++) {
- family = xml.getChild(i);
- //get family relations
- friend1 = family.getChild(0);
- int mate1 = friend1.getIntAttribute("mate1");
- friend2 = family.getChild(1);
- int mate2 = friend2.getIntAttribute("mate2");
- mum[i] = new Mother(1+i);
- //add friends
- mum[i].friends.add(mate1);
- mum[i].girl.friends.add(mate1);
- mum[i].friends.add(mate2);
- mum[i].girl.friends.add(mate2);
- }
- }
- void draw() {
- for (int i = 0; i < mum.length; i++) {
- mum[i].transmit();
- }
- }
- /*................................................*/
- int kids = 2;
- class Mother {
- Daughter girl;
- ArrayList boys;
- ArrayList friends = new ArrayList();
- int ref;
- Mother(int id) {
- girl = new Daughter(id);
- ref = id;
- boys = new ArrayList();
- for (int i=0; i<kids; i++) {
- boys.add(new Son(id, i, friends));
- }
- }
- void transmit() {
- girl.display();
- for (int i=0; i<boys.size(); i++) {
- Son maleChild = (Son) boys.get(i);
- maleChild.display();
- }
- }
- }
- /*................................................*/
- class Daughter {
- int id;
- ArrayList friends = new ArrayList();
- Daughter(int identity) {
- id = identity;
- }
- void display() {
- println(id + ": " + "girls friends" + friends);
- }
- }
- /*................................................*/
- class Son {
- int id;
- int who;
- ArrayList friends = new ArrayList();
- Son(int identity, int whom, ArrayList buddies) {
- id = identity;
- who = whom;
- friends = buddies;
- }
- void display() {
- println(id + ": " + who + ": " + "boys friends" + friends);
- //remove the friend of one son
- if ((id == 2) && (who == 0)) {
- println(id + ": " + who + ": i've had a fight with friend 3!");
- for (int i=0; i<friends.size(); i++) {
- int fallenOutWith = (Integer) friends.get(i);
- if (fallenOutWith == 3) {
- friends.remove(new Integer(fallenOutWith));
- }
- }
- }
- }
- }
XML file data:
- <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
- <families>
- <family>
- <friend mate1="2"/>
- <friend mate2="3"/>
- </family>
- <family>
- <friend mate1="1"/>
- <friend mate2="3"/>
- </family>
- <family>
- <friend mate1="1"/>
- <friend mate2="2"/>
- </family>
- </families>
1