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.


  • Please provide your code.


  • To make them different you cant say


    Instead use arraycopy

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