Saturday, 15 June 2013

arrays - method is changing my class variable char[][] but i dont know why java -



arrays - method is changing my class variable char[][] but i dont know why java -

i didnt know phone call question sense free edit it...

basically have char[][] class variable "canvasarray[][]"

it has been populated in different method characters represent letter on canvas (meant ascii art)

i have method flips image horizontally

void mirrorhorizontally() { char[][]horizontalimage = canvasarray; (int = 0; < height /2; i++) { for(int j = 0; j < width; j++) { horizontalimage[height - (i+1)][j] = horizontalimage[i][j]; horizontalimage[i][j] = horizontalimage[height - (i+1)][j]; } } printpicture(horizontalimage); }

from can see im setting char[][] horizontal image values of canvasarray seems not because if print canvasarray instead showing me same.

i need canvas array unchanged because have other methods need utilize too

any ideas??

thanks in advance

edit: have answered im afraid reply kind of combination of have said dont know whos accept??

i go like:

void mirrorhorizontally() { char[][]horizontalimage = new char[horizontalimage.length][horizontalimage[0].length]; // assumes lines have same length (int = 0; < height /2; i++) { for(int j = 0; j < width; j++) { horizontalimage[height - (i+1)][j] = canvasarray[i][j]; horizontalimage[i][j] = canvasarray[height - (i+1)][j]; } } printpicture(horizontalimage); }

this way i'm creating new buffer (not reffering , modifying old one) , using straight populate new buffer

java arrays char

No comments:

Post a Comment