我正在为这段代码获取一个数组超出绑定的异常

时间:2014-04-06 18:59:33

标签: java arrays image-processing multidimensional-array

public  CompressImage(){

}


// compress image method
public static short[] compress(short image[][]){

    // get image dimensions
    int imageLength = image.length;   // row length
    int imageWidth = image[0].length; // column length    


    // convert vertical to horizontal 

    // store transposed Image
    short[][] transposeImage = new short[imageWidth][imageLength];

    // rotate by +90
    for (int i = 0; i < imageWidth; i++)
    {
          for (int j = 0; j < imageLength; j++)
          {
             short temp = image[i][j];
             transposeImage[i][j] = image[j][i];
             transposeImage[j][i] = temp;
         }
    }

1 个答案:

答案 0 :(得分:0)

short temp = image[i][j];
transposeImage[i][j] = image[j][i];
transposeImage[j][i] = temp;

你为什么要在这里交换?这没有意义 - transposeImage是一个新矩阵,因此您不必进行现场编辑。如果imageWidth != imageLength,请确保打破这一点 - 看看你能否找出原因。

实际上,你甚至没有交换。上面的三行相当于:

transposeImage[i][j] = image[j][i];
transposeImage[j][i] = image[i][j];

嵌套for循环的主体应该只是:

transposeImage[i][j] = image[j][i];