java - java中的旋转字符串数组

标签 java arrays rotation tetris

我如何在java中为我正在制作的俄罗斯方 block 游戏旋转字符串数组。例如字符串数组

[
"JJJJ",
"KKKK",
"UUUU"
]

会变成

[
"UKJ",
"UKJ",
"UKJ",
"UKJ"
]

我可以使用此代码通过字符矩阵来完成此操作

public char[][] rotate(char[][] toRotate)
{
    char[][] returnChar = new char[toRotate[0].length][toRotate.length];
    for(int rows = 0; rows<toRotate.length; rows++)
    {
        for(int cols = 0; cols<toRotate[0].length; cols++)
        {
            returnChar[cols][toRotate.length-1-rows]=toRotate[rows][cols];
        }
    }
    return returnChar;
}

最佳答案

使用数组字符串与您所做的类似:

public static String[] rotate(String [] toRotate)
  {

      String [] returnChar = new String[toRotate[0].length()];
      String [] result = new String[toRotate[0].length()];
      Arrays.fill(returnChar, "");

      for(int rows = 0; rows<toRotate.length; rows++)
          for(int cols = 0 ; cols < toRotate[rows].length(); cols++)
              returnChar[cols] = returnChar[cols] + toRotate[rows].charAt(cols);

      for(int i = 0; i < returnChar.length; i++)
          result[i] =  new StringBuffer(returnChar[i]).reverse().toString();

      return result;
  }

我遍历数组toRotate上每个String中的所有char,连接这个字符(toRotate[rows].charAt( cols)) 到数组 returnChar

上的每个字符串 returnChar[cols]

关于java - java中的旋转字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13635697/

相关文章:

java - 这段代码中的两个类指的是什么?

java - 服务器 : Null java. sql.SQLException 参数索引超出范围(1 > 参数数量,为 0)

c - 数组中的元素太多!

c++ - 二维数组中的自动初始化对象?

android - 旋转后 TextView 被剪切

math - 在 3D 空间中存储方向的快速有效方法?

java - 如何在 android 谷歌地图版本 2 中的 2 个地理点之间画线?

java - ProcessBuilder/Runtime.exec() 与 Weka 命令行演示特殊行为

javascript - 检查多维数组长度

iphone - iPad旋转锁定问题