6

I have a problem in Eclipse. Why is the value of oldList different in LogCat while I don't change it between the tow Log command?

First I have an initialize method:

private void initialize() {

    list[0][0] = 2;
    list[0][1] = 4;
    list[1][0] = 3;
    list[1][1] = 7;

    oldList = list;

    going();
}

and in the going method, I printed oldList twice :

private void going() {

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            Log.i("Log", "oldList = " + oldList[i][j]);
        }
    }
    Log.i("Log", "---------------------------");

    //    ----------------------------------------------------
    list[0][0] = 0;
    list[0][1] = 5;

    list[1][0] = 0;
    list[1][1] = 0;
    //    ----------------------------------------------------

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            Log.i("Log", "oldList = " + oldList[i][j]);
        }
    }
}

but the two results is different in LogCat :

oldList = 2
oldList = 4
oldList = 3
oldList = 7
---------------------------
oldList = 0
oldList = 5
oldList = 0
oldList = 0

While i don't change it between the two logs. I just change the value of list, not oldList. Why does the output change?

Holger Just
  • 52,918
  • 14
  • 115
  • 123
Reza
  • 151
  • 1
  • 9
  • [Is Java “pass-by-reference” or “pass-by-value”?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) relevant – zapl Oct 07 '15 at 12:17

3 Answers3

4

Both list and oldlist refer to the exact same object. When you run

oldlist = list

you have two different "names" referring to the exact same object in memory. When you assign an object (in your case the array) to a variable, this object will not be copied.

Thus, as you change the list array in your going method, you are changing the object referred to by both list and oldlist.

Holger Just
  • 52,918
  • 14
  • 115
  • 123
  • Thanks very much for your answer . so if i want to protect the value of oldList i must assign each cells of list to the similar cells in oldList , not assign like : oldList = list ; – Reza Oct 07 '15 at 13:01
  • 1
    You can copy your existing `list` array instead of just assigning it. See [Make copy of array Java](http://stackoverflow.com/q/5785745/421705) for details. – Holger Just Oct 07 '15 at 13:24
3

oldlist and list are two references that point to the same array.

Andres
  • 10,561
  • 4
  • 45
  • 63
1

you can try this :

private void initialize() {
   list[0][0] = 2;
   list[0][1] = 4;
   list[1][0] = 3;
   list[1][1] = 7;

   // simply
   oldList[0] = list[0].clone();
   oldList[1] = list[1].clone();

   // or in a greater 2D arrays
   for (int i = 0; i < list.length; i++) {
      System.arraycopy(list[i], 0, oldList[i], 0, list[0].length);
   }

   going();
}
  • @Reza: Hello, if the answer is ok for you, can you accept it plz? – kayosoufiane Oct 15 '15 at 08:14
  • Hello , answer is ok but what is your mean about accept ? – Reza Oct 21 '15 at 13:17
  • Thanks . it's done. but it was my first question and i recieved answer soon. but my second question has not been answered yet . why ? it is about animation of 2048 Game . – Reza Oct 23 '15 at 08:09