I am trying to convert a 2D array. The content of a horizontal row is transferred to vertical column.
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];
}
}
}
Here's an example of how the rotation works, the rotation is really only the middle for loop,(below the comment), I also made it for both directions with output:
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();
}
OUTPUT:
A B C
D E F
G H I
Rotated
C F I
B E H
A D G
And if you want to rotate it the second way.
replace the for loop of : //HERE IS THE ROTATION
by this:
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];
}
}
OUTPUT:
A B C
D E F
G H I
Rotated
G D A
H E B
I F C
if lowLength is 29, you only can access to 28. because index of for starts from 0.
change
i < arrayWidth --> i < arrayWidth - 1
i < arrayLength --> i < arrayLength - 1
It is just a possibility.
Change below line :
short[][] transposedImage = new short[arrayLength][arrayWidth];
instead of
short[][] transposedImage = new short[arrayWidth][arrayLength];
This is your code after change :
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];
}
}
Assuming arrayLength & arrayWidth
to be the length & width
of decompressedImage
, the loop construct
for (int i=0;i<arrayWidth; i++){
for (int j=0;j<arrayLength; j++){
transposedImage[j][i]=decompressedImage[i][j];
}
}
is actually in accordance with the dimensions of transposedImage
and NOT of decompressedImage
ie your loop variables i & j
take the dimensions of transposedImage.
Thus change the statement
transposedImage[j][i]=decompressedImage[i][j];
to
transposedImage[i][j]=decompressedImage[j][i];
I guess that should help.
you defined the arre transposedImage
of dimension [arrayWidth][arrayLength]
but, while you are accessing the array, you are looping until [arrayLength][arrayWidth]
, notice the i
and j
loop of yours. according the loop, it should be,
transposedImage[i][j]=decompressedImage[j][i];
and NOT
transposedImage[j][i]=decompressedImage[i][j];
corrected code segment,
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];
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.