How do I use arraycopy?

edited April 2018 in How To...

So, i'm trying to get the differences between two boolean arrays, (I have a setter, i'm trying to make a screen) but if I change one, it changes the other.

Answers

  • Please provide your code.

    Kf

  • To make them different you cant say

    array1=array2

    Instead use arraycopy

    https://www.tutorialspoint.com/java/lang/system_arraycopy.htm

  • But i did

  • i mean i used arraycopy

  • public void updateScreen(boolean[][] s){ arrayCopy(s,screen); }

  • Too short

    Can you provide an entire sketch demonstrating the issue

    Here you copy s into screen....

  • A 2D array is an 'array of arrays' so when you use arrayCopy(s,screen); you are copying the array references from s to screen.

    This means that screen[0] is the same array as s[0] so when you use

    s[0][5] = true;
    

    you are also setting screen[0][5] to true and visa-versa.

    Assuming that s and screen have both been created and have the same dimensions then this will copy the individual elements

    public void updateScreen(boolean[][] s) {
      for (int idx = 0; idx < s.length; idx++) {
        arrayCopy(s[idx], screen[idx]);
      }
    }
    
  • ohhhhhhhhhhhhhhh

Sign In or Register to comment.