java - Rotating a String or char clockwise by 90 degrees -


folks, i'm having hard time rotating shape clockwise 90 degrees. i'm having troubles completing it. if there shape:

.t. ttt 

the method rotateby90() rotates above shape 90 degrees clockwise, it'll following output:

t. tt t. 

the shapes of string type. here have , im pretty sure im doing wrong. method can done using either char[][], or char[], or string[], or string[][]. issue rotateby90() void method. smb please me rotation algorithm? in advance!

import java.util.*;  public class createshape {      private int height;     private int width;     private char dc;     private rotation initialpos;     private rotation nextpos;     private char[][] shape = new char[height][width];      string[] shapelayout = new string[height];     string[] rotatedarray;  public createshape(int height, int width, char dc)     {         this.height = height;         this.width = width;         this.dc = dc;         initialpos = rotation.cw0;     }      public void rotateby90()      {         nextpos = initialpos.next();          string newlayout = "";          int count = 0;         string[] newmatrixcolumns= shape.split("\n");         while (count < shape.split("\n")[0].length()) {             (int = newmatrixcolumns.length - 1; > -1; i--) {                 newlayout += newmatrixcolumns[i].charat(count);             }              newlayout = newlayout + "\n";             count++;         }      } 

i don't quite approach , difficulties lie. don't why have many unused fields. may want set width , height fields final, shouldn't change in case.

personally think far easiest use char[][]. have iterate initial char[][] , place element @ appropriate place in new char[][]. in method you'd have like

public void rotateby90()  {     // bla bla     char[][] tempshape = new char[width][height];      for(int j = 0; j < width; j++) {         for(int = 0; < height; i++) {             tempshape[...][...] = myshape[i][j]; //i'll leave exercise         }     }      myshape = tempshape; } 

word of caution: careful strings. many string operations involve creating other strings (split, substring, concatenation etc). if them in long loop, may run out of memory quickly.


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -