/**
* @author lautturi.com
* Java example: how to print rows as columns in java 2d array
*/
import java.util.*;
public class Lautturi {
public static void main(String[] args) {
int arr[][] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8, 15 }, { 9, 10, 11, 12 } };
int maxlen = 0;
for (int row = 0; row < arr.length; row++) {
maxlen = arr[row].length>maxlen? arr[row].length:maxlen;
}
for (int col = 0; col < maxlen; col++) {
for (int row = 0; row < arr.length; row++) {
if(col<arr[row].length)
System.out.printf("%5d", arr[row][col]);
else
System.out.printf("%5s","");
}
System.out.println();
}
}
}
output:
1 5 9
2 6 10
3 7 11
4 8 12
15