hellohai
hellohai

Reputation: 1

Java copy object and update copied object

i am copying object tt to ttt and i want to make change to ttt only but when i update ttt dunno why it update my tt along???? it's my makechange() function got problem?

this is main class:

package test;

public class Test {

public static void main(String[] args) {
    Solution sol;
    sol= new Solution();

    sol.add();
    sol.copy();
    //this makechange function only update ttt only!!
    sol.makechange();
    sol.disOld();
    System.out.println("===============");
    sol.disNew();

}
}

this is new class:

package test;

import java.util.ArrayList;
import java.util.List;

public class Solution {
Object[][] tt=new Object[2][2];
Object[][] ttt=new Object[2][2];
List l = new ArrayList<>();

public void add(){
    l.add(100);
    tt[0][0]=l;
    l = new ArrayList<>();
    l.add(123);
    tt[0][1]=l;
    l = new ArrayList<>();
}

public void disOld(){
    for(int i=0; i<tt.length; i++){
        for(int j=0; j<tt[i].length; j++){
            System.out.println(tt[i][j]);
        }
    }
}

public void copy(){
    ttt=tt;
}

public void makechange(){
    l.add(99);
    ttt[1][0]=l;
}

public void disNew(){
    for(int i=0; i<ttt.length; i++){
        for(int j=0; j<ttt[i].length; j++){
            System.out.println(ttt[i][j]);
        }
    }
}

}

this is my output:

[100]
[123]
[99]
null
===============
[100]
[123]
[99]
null

this is my expected output should be like this:

[100]
[123]
null
null
===============
[100]
[123]
[99]
null

Upvotes: 0

Views: 3015

Answers (3)

m0skit0
m0skit0

Reputation: 25874

Because = just copies the reference (pointer), not the object, so the real object you're referencing is the same. I suggest you use a copy constructor as explained here.

You can read a more extended explanation here (it's about ArrayList, but you can extrapolate to any other object).

Extract from that answer:

b = a

Keep in mind this lines DOES NOT copy the whole list a to b, but only copies the reference to the list. Now both a and b reference (point) to the same List. So it doesn't matter if you use a.add() or b.add(), you'll be modifying the same List.


To help you understand the above, check the following diagram

enter image description here

The left diagram corresponds to when you do Object[][] tt=new Object[2][2];. You can see that you create an Object[2][2] instance in memory, which is the circle, and you assign a pointer (reference) to it called tt, which is the rectangle.

The right diagram corresponds to when you do ttt = tt. This means: "make ttt point to same object as tt". It does not copy anything for you. So now both tt and ttt point (reference) the same object instance in memory. So if you use tt or ttt, you will be modifying the same object instance.

I hope this clarifies what you're doing. As for fixing it, you should copy each element of the array one by one as explained in Duncan's answer. More generically, you should use a copy constructor to copy objects as I linked above.

Upvotes: 4

javaHunter
javaHunter

Reputation: 1097

The = just makes both references point to the same object. If you want to make a copy(in general), then have a look at Cloninng(Not Recommended) in Java or consider a Copy-Constructor.

To solve your problem, change your copy method to the following:

public void copy(){
    for(int i=0; i<tt.length; i++)
        for(int j=0; j<tt[i].length; j++)
             ttt[i][j]= tt[i][j];
}

Upvotes: 2

Duncan Jones
Duncan Jones

Reputation: 69399

This is not how you copy an array:

public void copy(){
    ttt=tt;  // booo, hisss, etc.
}

After this method executes, ttt points at exactly the same array as tt. Changes made to that array are visible through both variables.

You need to properly copy the array, e.g. using a technique from How do I copy a 2 Dimensional array in Java?.

Upvotes: 1

Related Questions