java - Getting indexOutOfBOundException 0 -
why keep getting indexoutofboundexception
0 on following line?:
rotatedshape[r][c] = shape[c][height-1-r];
the method rotatecw()
turns char[][]
90 degrees clockwise im keep getting error. smb please take look? thank you!
public static void main(string[] args) { createshape temp = new createshape(10,10, 'a', new char[][]{{'x','.','.'}, {'.','.','x'}, {'x','.','x'}}, "x . .\n" + ". . x\n" + "x . x"); temp.rotatecw(); system.out.println(temp); } public class createshape implements shape { private string templayout = ""; private string layout; private int height; private int width; private char dc; private rotation initialpos; private rotation nextpos; private char[][] shape; private char[][] rotatedshape = new char[height][width];// = new char[shape.length][shape[0].length]; public createshape(int height, int width, char dc, char[][] charlayout, string layout) { this.height = height; this.width = width; this.dc = dc; this.shape = charlayout; this.layout = layout; initialpos = rotation.cw0; } public void rotatecw() { // string templayout = ""; nextpos = initialpos.next(); /*for(int r = 0; r < height; r++) for(int c = 0; c < width; c++) { rotatedshape[c][height-1-r] = shape[r][c]; layout += rotatedshape[c][height-1-r]; }*/ for(int r = 0; r < height; r++) for(int c = 0; c < width; c++) { rotatedshape[r][c] = shape[c][height-1-r]; templayout += rotatedshape[r][c]; } } }
createshape temp = new createshape(10,10, 'a', new char[][]{{'x','.','.'}, {'.','.','x'}, {'x','.','x'}}, "x . .\n" + ". . x\n" + "x . x");
you passing in width , height of 10, array 3x3. proceed loop through 3x3 array (shape
) based on width/height of 10.
for(int r = 0; r < height; r++) for(int c = 0; c < width; c++) { rotatedshape[r][c] = shape[c][height-1-r]; templayout += rotatedshape[r][c]; } }
10 greater 3. cause indexoutofbounds
exception.
Comments
Post a Comment