We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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.
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.
arrayCopy(s,screen);
s
screen
This means that screen[0] is the same array as s[0] so when you use
screen[0]
s[0]
s[0][5] = true;
you are also setting screen[0][5] to true and visa-versa.
screen[0][5]
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
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 froms
toscreen
.This means that
screen[0]
is the same array ass[0]
so when you useyou are also setting
screen[0][5]
to true and visa-versa.Assuming that
s
andscreen
have both been created and have the same dimensions then this will copy the individual elementsohhhhhhhhhhhhhhh