//*****************************************************************************
// Transpose a 2dim array                            
//*****************************************************************************
public class transpose  
{
    public static void main(String[] args)
    {
        int[ ][ ] fromArray = { {11, 12, 13, 14, 15},       //define a 2 dim array
                                {21, 22, 23, 24, 25, 26},               
                                {31, 32, 33, 34, 35} };

        System.out.println("SOURCE ARRAY");
        printArray(fromArray);

        int[ ][ ] toArray = transpose(fromArray);
        
        System.out.println("NEW ARRAY");
        printArray(toArray);
    }

//----------------------------------------------------------------------
// Transpose a 2 dimensional array
//----------------------------------------------------------------------
    public static int[][] transpose(int[][] fromArray)
    {

        int numRows = fromArray.length;                     //compute number of rows
        int numCols = 0;                                    //for number of columns

        for (int[] row : fromArray)                         //loop through all rows                     
            if (row.length > numCols)                       //take the size of the longest row                              
                numCols = row.length;                       //this becomes the number of columns        

        System.out.println("\nTransposing Rows and Columns size: "+ 
                               numRows +"rows x "+ numCols +"cols");

        int[][] toArray = new int[numCols][numRows];        //create new array (notice cols then rows)

        int row=0;
        int col=0;

        for (int[] fromRow : fromArray)             //loop through the rows
        {
            row=0;
            for (int fromCol : fromRow)             //loop through the columns
            {
                toArray[row][col] = fromCol;        //copy from fromArray to toArray
                row++;        
            }
            col++;
        }

        return(toArray);
    }

//----------------------------------------------------------------------
// Print a 2 dimensional array
//----------------------------------------------------------------------
    public static void printArray(int[][] array)
    {
        for (int[] row : array)                     //loop through the rows
        {
            for (int col : row)                     //loop through the columns
                System.out.print(col + "\t");       //print
            System.out.println();
        }
    }
}