【问题标题】:Trying to transpose an array, but getting array out of bounds尝试转置数组,但数组超出范围
【发布时间】:2014-05-23 14:35:57
【问题描述】:

我正在尝试转换二维数组。水平行的内容被转移到垂直列。

public static void change(short image[])
{

   int arrayLength = 29; // row length
   int arrayWidth = 10; // column length
   // array with values
   short[][] decompressedImage = new short[arrayLength][arrayWidth]; 

   // array to store transposed image
   short[][] transposedImage = new short[arrayWidth][arrayLength];

   // store transposed values

   for (int i=0;i<arrayWidth; i++){
      for (int j=0;j<arrayLength; j++){
         transposedImage[j][i]=decompressedImage[i][j];
      }
   }
}

【问题讨论】:

  • 你的意思是要旋转数组?
  • 是的,旋转它! -90度
  • decompressedImage 或 transposedImage 的 arrayLength & arrayWidth 维度是??
  • @ray,我可以在网上找到这些东西,但我正在尝试解决我的代码片段中的问题。

标签: java for-loop matrix multidimensional-array


【解决方案1】:

这是一个旋转如何工作的例子,旋转实际上只是中间的 for 循环,(在评论下方),我也做了两个方向的输出:

    String[][] matrix = {{"A","B","C"},{"D","E","F"},{"G","H","I"}};
String[][] tmp = new String[matrix.length][matrix.length];

    //PRINT BEFORE ROTATION
for(int row=0; row<matrix.length;row++){
    for(int col=0; col<matrix.length;col++){
        System.out.print(matrix[row][col] + "  ");
    }
    System.out.println();
}

    //HERE IS THE ROTATION
for(int row=0; row<matrix.length;row++){
    for(int col=matrix.length-1; col>=0;col--){
        tmp[col][row] = matrix[row][matrix.length-1-col];
    }
}

    //PRINT AFTER ROTATION
System.out.println("Rotated");
for(int row=0; row<matrix.length;row++){
    for(int col=0; col<matrix.length;col++){
        System.out.print(tmp[row][col] + "  ");
    }
    System.out.println();
}

输出:

A  B  C  
D  E  F  
G  H  I  

Rotated

C  F  I  
B  E  H   
A  D  G  

如果你想以第二种方式旋转它。

替换//HERE IS THE ROTATION的for循环

通过这个:

    for(int row=matrix.length-1; row >= 0; row--){
    for(int col=0; col<matrix.length;col++){
        tmp[col][row] = matrix[matrix.length-1-row][col];
    }
}

输出:

A  B  C  
D  E  F  
G  H  I  

Rotated

G  D  A  
H  E  B  
I  F  C  

【讨论】:

    【解决方案2】:

    您定义了维度[arrayWidth][arrayLength] 的arre transposedImage,但是,当您访问该数组时,您将一直循环到[arrayLength][arrayWidth],请注意您的ij 循环。根据循环,应该是,

    transposedImage[i][j]=decompressedImage[j][i];
    

    不是

    transposedImage[j][i]=decompressedImage[i][j];
    

    更正的代码段,

    short[][] decompressedImage = new short[arrayLength][arrayWidth];
    short[][] transposedImage = new short[arrayWidth][arrayLength];
    for (int i=0;i<arrayWidth; i++){
      for (int j=0;j<arrayLength; j++){
         transposedImage[i][j]=decompressedImage[j][i];
      }
    }
    

    【讨论】:

      【解决方案3】:

      假设arrayLength &amp; arrayWidthlength &amp; widthlength &amp; width,循环构造

      for (int i=0;i<arrayWidth; i++){
        for (int j=0;j<arrayLength; j++){
           transposedImage[j][i]=decompressedImage[i][j];
        }
      }
      

      实际上是按照transposedImage的尺寸而不是decompressedImage的尺寸,即您的循环变量 i &amp; j 采用 transposedImage 的尺寸。

      因此改变语句

       transposedImage[j][i]=decompressedImage[i][j];
      

      transposedImage[i][j]=decompressedImage[j][i];
      

      我想这应该会有所帮助。

      【讨论】:

        【解决方案4】:

        更改以下行:

         short[][] transposedImage = new short[arrayLength][arrayWidth];  
        

        而不是

        short[][] transposedImage = new short[arrayWidth][arrayLength];  
        

        这是您更改后的代码:

        int arrayLength = 29; // row length
        int arrayWidth = 10; // column length
        short[][] transposedImage = new short[arrayLength][arrayWidth];
        short[][] decompressedImage = new short[arrayWidth][arrayLength];
        
        // store transposed values
        for (int i=0;i<arrayWidth; i++){
          for (int j=0;j<arrayLength; j++){
             transposedImage[j][i]=decompressedImage[i][j];
          }
        }
        

        【讨论】:

        • @yagnesh,我想我一开始并不清楚。再次检查我的代码。
        • @user3504305 比你需要更改transposedImage[i][j]=decompressedImage[j][i]; 这一行
        【解决方案5】:

        如果 lowLength 为 29,则只能访问 28。 因为 for 的索引从 0 开始。

        改变

        i < arrayWidth --> i < arrayWidth - 1
        i < arrayLength --> i < arrayLength - 1
        

        这只是一种可能。

        【讨论】:

        • 你确定你说的是什么??我相信 i
        • @user3504305 你是对的。我的错。对不起!
        猜你喜欢
        • 2021-06-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多