# Copy objects correctly

**URL:** <https://discourse.processing.org/t/copy-objects-correctly/29110>\
**Category:** Coding Questions\
**Created:** [April 4, 2021, 8:11am UTC](https://discourse.processing.org/t/copy-objects-correctly/29110 "2021-04-04T08:11:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)\
**Post date:** [April 4, 2021, 8:11am UTC](https://discourse.processing.org/t/copy-objects-correctly/29110/1 "2021-04-04T08:11:18Z")

</div>

I am currently trying to make a program to play chess.

I have the following problem.  
I want to duplicate my object from my board-class and check and evaluate the position.

However when I apply a method to a direct copy of the object it also applies it to the original object.

How can I fix that?

---

<div class="post-metadata">

**Author:** ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)\
**Post date:** [April 4, 2021, 8:56am UTC](https://discourse.processing.org/t/copy-objects-correctly/29110/2 "2021-04-04T08:56:44Z")

</div>

I hope this helps:

> [@How to clone Object without modifying original?](https://discourse.processing.org/t/how-to-clone-object-without-modifying-original/28496):
>
> I have an array of a class, that I want to clone. But if I modify the clone, the original gets modified to. Now is the question, how can i clone an array of classes without it modifying the original?

---

<div class="post-metadata">

**Author:** ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)\
**Post date:** [April 4, 2021, 9:25am UTC](https://discourse.processing.org/t/copy-objects-correctly/29110/3 "2021-04-04T09:25:39Z")

</div>

I already tried something simular and it didn’t work.

If it helps this is my code

```auto

    ArrayList<Integer>[] m=getmoves(white_move);
    float besteval=-200;
    int in=-1;
    ArrayList<Piece> backup[]=new ArrayList[2];
    backup[0]=black;
    backup[1]=white;
    for (int i=0; i<rek[0].size(); i++) {
      Board bn=(new Board(white,black,white_move)).moved(m[0].get(i), m[1].get(i), m[2].get(i), m[3].get(i));
      if (b.suchen((int)m[2].get(i), (int)m[3].get(i))!=null) 
     float ev=((white_move)?-1.0:1.0)*bn.evaluate_position();
      if (ev>besteval) {
        besteval=ev;
        in=i;
      }
    }
    int rand=int(random(0, m[0].size()));
    if (in<0) in=int(random(0, m[0].size()));
    white=backup[0];
    black=backup[1];
    move_request(m[0].get(in), m[1].get(in), m[2].get(in), m[3].get(in));
  }

```

---

<div class="post-metadata">

**Author:** ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)\
**Post date:** [April 4, 2021, 9:55am UTC](https://discourse.processing.org/t/copy-objects-correctly/29110/4 "2021-04-04T09:55:25Z")

</div>

thanks for posting the code (it really helps to do so in the first place) ! But it’s hard to follow without the context. Could you point to the code where you “tried something similar” ?

As Chrisir wrote, implementing a copy function like

```auto
class ClassName {
  ...
  ...
  ClassName copy () {
    ClassName newElement = new ClassName(); 

    newElement.x= x; 
    newElement.y= y; 
    newElement.col= col; 
    // copy all properties you need here

    return newElement;  
  }
}

```

would be a solution. Or, you can define a constructor so that passing the “original” instance as an argument of the constructor will copy all the member variables.

```auto
class ClassName {
  ...
  ...
  ClassName (ClassName original) {
    x = original.x;
    y = original.y;
    col = original.col;
    // copy all properties you need here
  }
}

```

Note that if you have a property that is an instance of a class (e.g., ArrayList) inside the class then you have to somehow copy that object as well. This is why “deep” copy cannot be generalized especially when you have a circular dependency.

---

<div class="post-metadata">

**Author:** ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)\
**Post date:** [April 4, 2021, 10:18am UTC](https://discourse.processing.org/t/copy-objects-correctly/29110/5 "2021-04-04T10:18:47Z")

</div>

This is the version I use right now:

```auto
  Board copy() {
    return new Board(white,black,white_moves);
  }

```

The approach I chose before is

```auto
  Board copy() {
    Board nb=new Board();
nb.white.clear();
nb.black.clear();
for(int i=0;i<white.size();i++) nb.white.add(white.get(i));
for(int i=0;i<black.size();i++) nb.black.add(black.get(i));
nb.white_moves=white_moves;
return nb;
  }

```

---

<div class="post-metadata">

**Author:** ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)\
**Post date:** [April 4, 2021, 10:22am UTC](https://discourse.processing.org/t/copy-objects-correctly/29110/6 "2021-04-04T10:22:51Z")

</div>

ok I think the problem could be that `white` and `black` are ArrayLists. If that is the case, as I noted above, the cloned `Board` shares the same `white` and `black` which can be an issue. You need to make new lists and copy each element from the list (and… if these elements seem to be instances of `Piece`, so you need to make `copy` method in this class as well).

---

<div class="post-metadata">

**Author:** ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)\
**Post date:** [April 4, 2021, 10:49am UTC](https://discourse.processing.org/t/copy-objects-correctly/29110/7 "2021-04-04T10:49:52Z")

</div>

I tried copying the ArrayLists by just adding the Pieces but as new objects and it worked!  
Thanks for your help!
