Copy Matrix without linking them forever

edited November 2015 in How To...

Hey, how is it going?

Im doing an android app for making pixelArt. Im using 2 16x16 matrixes of a Pixel object. Basicaly, inside the object there is: int x, y, tam; color cor; boolean pixel = false;

x and y are normal position values, tam is the size of the square, cor is the color of that pixel, the boolan is because when its true it draws using a universal brush color, when false it draws using a universal background color.

So, what Im trying to implement is a button to copy matrix 1 to matrix 2 (remembering there are two 16x16 matrixes of this object). It only shows 1 matrix at a time, and the idea is to make 2 frames animations, so I need this feature so the user does not have to make the same draw two times just to change one small detail wich is gonna be animated.

I have tried some different ways and whats happening is that it does copy, but after copying they stay linked, and what ever change I draw in one is made instantly in the other. Here`s what Ive tried:

Matrix2 = Matrix1;

or

for (int i = 0; i < 16; i++){ for (int j = 0; j < 16; j++){ matrix2[i][j].pixel = matrix1[i][j].pixel; matrix2[i][j].cor = matrix1[i][j].cor; }

or

arrayCopy(Matrix1, Matrix2);

all of them give the same result, instantly copies the matrix to the other, but when I change something and go back to the other, it has also changed the other, even without pressing to copy again.

I didnt paste the code because its too big, its already working for android and only this feature missing, posting the code would maybe distract from the problem (too many variables and other stuff unrelated to this problem) but if necessary Ill post. We can face this as a simple problem, when you use this comands above seems like it does not only copy the values between the matrixes but it link them and future changes affect one another.

Answers

  • edited November 2015

    I tought this could be confuse so I have prepared a test, heres the code

    int[][] m1 = new int[2][2];
    int[][] m2 = new int[2][2];
    
    int x, y;
    
    void setup() {
      size(500, 500);
    }
    
    void draw() {
      if (mousePressed == false) {
        x = 10;
        y = 100;
      }
    
      if (mousePressed == true) x = 20;
      for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
          m1[i][j] = x;
          m2[i][j] = y;
          x += 5;
          y += 5;
        }
      }
      print("Matriz 1: \n");
      for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
          print(m1[i][j]);
          print("\t");
        }
        println();
      }
      print("Matriz 2: \n");
      for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
          print(m2[i][j]);
          print("\t");
        }
        println();
      }
    }
    
    void keyPressed() {
      if (key == 'q')  m2 = m1;
      else if (key == 'w') {
        for (int i = 0; i < 2; i++) {
          for (int j = 0; j < 2; j++) {
            m2[i][j] = m1[i][j];
          }
        }
      }
      else if (key == 'e') arrayCopy(m1, m2);
    }
    

    after copying using m2 = 1 or arrayCopy(m1, m2); when i press the mouse both matrixes are affected by the increment of the X value, and it shouldnt be that way, should it?

    using the for loop nothing even happens.. What am I missing?

    thanks in advance for trying to understand this messy discussion. Bad english and noob skillzzzz

    =(

  • Answer ✓

    The for-loop would work. But you overwrite the values in draw(), so you never see that the values have actually been copied. Try to move that part to mousePressed() for example.

  • Answer ✓

    To copy a matrix you need to understand what this statement means -

    int[][] m1 = new int[2][2];

    What is m1?

    Those not familiar with the inner workings of Java would say it is a '2x2 array of integers' but in reality it is not.

    In Java m1 is an object reference to a 2D array of integers, it is NOT the actual array. In fact the 2D array could be any size we like. For instance after these statements

    int[][] m1 = new int[2][2];
    m1 = new int[20][10];
    

    m1 is still an object reference to a 2D array of integers, but the array is now 20x10

    So now back to your code

    int[][] m1 = new int[2][2];
    int[][] m2 = new int[2][2];
    
    m2 = m1;
    

    So after lines 1 and 2 we have 2 object references m1 and m2 both of which reference a separate 2D array of integers

    [m1 ref] ______> 2x2 array
    [m2 ref] ______> 2x2 array
    

    but then we have line 4. Here we copy the object reference m1 into the object reference m2 so we have

    [m1 ref] ______> 2x2 array
    [m2 ref] __|
    

    Now both m1 and m2 refer to the same array, so changing m1 also changes m2 and vice-versa.

    Of course it begs the question what happened to the second array? Since there is no reference to the array then it will be deleted from memory by Java's garbage collector.

    The moral of the story is that to copy an array you must copy the array elements.

  • for loop works, thanks a lot for the answers and explanations

Sign In or Register to comment.