Java 正在获取indexOutOfBOundException 0

Java 正在获取indexOutOfBOundException 0,java,Java,为什么我在下一行中不断获得indexOutOfBOundException0?: rotatedShape[r][c] = shape[c][height-1-r]; 方法rotateCW()将char[][]顺时针旋转90度,但我不断得到错误。smb能看一下吗?谢谢大家! public static void main(String[] args) { CreateShape temp = new CreateShape(10,10, 'a', new char[

为什么我在下一行中不断获得
indexOutOfBOundException
0?:

rotatedShape[r][c] = shape[c][height-1-r]; 
方法
rotateCW()
char[][]
顺时针旋转90度,但我不断得到错误。smb能看一下吗?谢谢大家!

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];

            }
    }
}
publicstaticvoidmain(字符串[]args)
{
CreateShape temp=新的CreateShape(10,10,'a',
新字符[][{{'x','.','.'.},
{'.','.',x'},
{'x','.','x'},
“x..\n”
+“.x\n”
+“x.x”);
温度旋转系数();
系统输出打印项次(温度);
}
公共类CreateShape实现形状{
私有字符串tempLayout=“”;
私有字符串布局;
私人内部高度;
私有整数宽度;
私人区议会;
私人轮换;
私人轮换下一个方案;
私有字符[][]形状;
私有字符[][]rotatedShape=新字符[高度][宽度];//=新字符[形状.长度][形状[0].长度];
公共CreateShape(整数高度、整数宽度、字符dc、字符[][]字符布局、字符串布局)
{
高度=高度;
这个。宽度=宽度;
this.dc=dc;
this.shape=charLayout;
this.layout=布局;
initialPos=旋转.CW0;
}
公营房屋小组委员会(
{
//字符串tempLayout=“”;
nextPos=initialPos.next();
/*对于(int r=0;r
您通过的宽度和高度为10,但您的阵列只有3x3。然后根据10的宽度/高度继续循环3x3数组(
形状

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];

        }
}
for(int r=0;r

10大于3。这显然会导致
IndexOutOfBounds
异常。

在知道数组的高度和宽度之前,您正在实例化数组。具体来说,这三个声明是让你崩溃的原因:

private int height;
private int width;
private char[][] rotatedShape = new char[height][width];
Java有一个关于构造时未分配变量的特殊规则。如果未分配它们。对于原语和
boolean
,分别为0和
false
;对于对象,它是
null

您在这里所做的是有效地创建一个大小为(0,0)的数组,这就是为什么您在索引到它时会越界

我的直觉告诉我你不需要
高度
宽度
字段。相反,您要做的是声明
rotatedShape
——只需声明它,不要实例化它

private char[][] rotatedShape;
在构造器内部,当您知道高度和宽度时,只有这样才能正确地实例化数组

public CreateShape(int height, int width, char dc,
                   char[][] charLayout, String layout) {
    this.dc = dc;
    this.shape = charLayout;
    this.layout = layout;
    initialPos = Rotation.CW0;
    rotatedShape = new char[height][width];
}

请添加堆栈跟踪,其中
rotatedShape
已初始化?该错误告诉您一个数组维度未初始化为大于0的大小。与
形状相同
。你需要发布更多的代码,我们无法了解全部情况。另外,请进行堆栈跟踪。
rotatedShape
在哪里声明?
nextPos
在哪里声明?
模板布局在哪里声明?旋转在哪里。。。。类或枚举错误,代码更新!我尝试了一切,包括改变尺寸。将维度更改为3x3可能无法消除所有错误,但使用错误的维度肯定会增加错误。这可能是程序中的一个合法错误,但这不是答案。
rotatedShape
数组的声明还有一些非常错误的地方。
public CreateShape(int height, int width, char dc,
                   char[][] charLayout, String layout) {
    this.dc = dc;
    this.shape = charLayout;
    this.layout = layout;
    initialPos = Rotation.CW0;
    rotatedShape = new char[height][width];
}