0

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

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

@hellohai I've linked another answer of mine which is about the same subject. Copying objects in Java is not as easy as you might think at first.
Your answer is complicated and not related to object instead of lists
Lists and arrays are objects, same applies. Answer is not complicated, you're simply confusing things. I'll update my answer with a diagram so you can understand better.
@hellohai Updated, I hope this explains better why when you modify tt ttt is also modified. I cannot explain in a more simple way than this.
@hellohai I see that you were looking for a copy-paste solution rather than an explanation. You will get this issue again if you don't understand this, but hey that's your problem, isn't it? ;)
2

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];
}

4 Comments

Cloning is generally considered a bad idea.
What do u mean cloning is a bad idea??
It has some disadvantages, but don't worry about it. If you want to know exactly why check the book Effective Java(Must read book for every Java Developer) Item 11: Override clone judiciously
@hellohai Read the link.
1

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?.

5 Comments

i had try this before but it also show the same result
@hellohai Please update the code in your question to show what you tried.
@hellohai I don't think you tried what Duncan is talking about.
@hellohai Just adopt the solution shown in this answer. It's probably faster than you explaining what you tried.
@Duncan But understanding why his code fails is how he can avoid such mistakes in the future. I bet we will see him asking the same thing again :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.