I have a row-based multidimensional array:
/** [row][column]. */
public int[][] tiles;
I would like to transform this array to column-based
try this:
@Test
public void transpose() {
final int[][] original = new int[][] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
for (int i = 0; i < original.length; i++) {
for (int j = 0; j < original[i].length; j++) {
System.out.print(original[i][j] + " ");
}
System.out.print("\n");
}
System.out.print("\n\n matrix transpose:\n");
// transpose
if (original.length > 0) {
for (int i = 0; i < original[0].length; i++) {
for (int j = 0; j < original.length; j++) {
System.out.print(original[j][i] + " ");
}
System.out.print("\n");
}
}
}
output:
1 2 3 4
5 6 7 8
9 10 11 12
matrix transpose:
1 5 9
2 6 10
3 7 11
4 8 12
a bit more generic way:
/**
* Transposes the given array, swapping rows with columns. The given array might contain arrays as elements that are
* not all of the same length. The returned array will have {@code null} values at those places.
*
* @param <T>
* the type of the array
*
* @param array
* the array
*
* @return the transposed array
*
* @throws NullPointerException
* if the given array is {@code null}
*/
public static <T> T[][] transpose(final T[][] array) {
Objects.requireNonNull(array);
// get y count
final int yCount = Arrays.stream(array).mapToInt(a -> a.length).max().orElse(0);
final int xCount = array.length;
final Class<?> componentType = array.getClass().getComponentType().getComponentType();
@SuppressWarnings("unchecked")
final T[][] newArray = (T[][]) Array.newInstance(componentType, yCount, xCount);
for (int x = 0; x < xCount; x++) {
for (int y = 0; y < yCount; y++) {
if (array[x] == null || y >= array[x].length) break;
newArray[y][x] = array[x][y];
}
}
return newArray;
}
I´m just digging this thread up because i did not find a working solution in the answers, therefore i will post one to help anyone who searches for one:
public int[][] transpose (int[][] array) {
if (array == null || array.length == 0)//empty or unset array, nothing do to here
return array;
int width = array.length;
int height = array[0].length;
int[][] array_new = new int[height][width];
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
array_new[y][x] = array[x][y];
}
}
return array_new;
}
you should call it for example via:
int[][] a = new int[][] {{1,2,3,4},{5,6,7,8}};
for (int i = 0; i < a.length; i++) {
System.out.print("[");
for (int y = 0; y < a[0].length; y++) {
System.out.print(a[i][y] + ",");
}
System.out.print("]\n");
}
a = transpose(a); // call
System.out.println("");
for (int i = 0; i < a.length; i++) {
System.out.print("[");
for (int y = 0; y < a[0].length; y++) {
System.out.print(a[i][y] + ",");
}
System.out.print("]\n");
}
which will as expected output:
[1,2,3,4,]
[5,6,7,8,]
[1,5,]
[2,6,]
[3,7,]
[4,8,]
public int[][] getTranspose() {
int[][] transpose = new int[row][column];
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
transpose[i][j] = original[j][i];
}
}
return transpose;
}
import java.util.*;
public class TestClass {
public static void main(String args[] ) throws Exception {
Scanner in=new Scanner(System.in);
int isize=in.nextInt();
int jsize=in.nextInt();
int arr[][]=new int[isize][jsize];
int array[][]=new int[jsize][isize];
for(int i=0;i<isize;i++) {
for(int j=0;j<jsize;j++) {
arr[i][j]=in.nextInt();
}
System.out.println("\n");
}
for(int n=0;n<arr[0].length;n++) {
for(int m=0;m<arr.length;m++) {
array[n][m]=arr[m][n];
System.out.print(array[n][m]+" ");
}
System.out.print("\n");
}
}
}