Java Swap Rows And Columns ClockWise In Two Dimensional Array Example

If you have a two-dimensional array, and you want to swap the array rows and columns elements in both clockwise and anti-clockwise directions. This example will tell you how to implement it in java.

1. Swap Two Dimensional Array Rows Columns Example.

  1. SwapColRowInArray.java
    package com.dev2qa.java.basic.array;
    
    public class SwapColRowInArray {
    
        public static void main(String args[])
        {
            // Define an int two dimensional array.
            int array[][] = new int[][]{{15,14,13},{12,11,10},{7,8,9},{6,5,4},{3,2,1}};
    
            System.out.println("Original array data.");
            printArrayData(array);
    
            // Get clockwise swapped array.
            int newArrayClockWise[][] = swapArrayClockWise(array);
            System.out.println("New array data swapped clockwise.");
            printArrayData(newArrayClockWise);
    
            // Get anti-clockwise swapped array.
            int newArrayAntiClockWise[][] = swapArrayAntiClockWise(array);
            System.out.println("New array data swapped anti-clockwise.");
            printArrayData(newArrayAntiClockWise);
    
        }
    
        /* Print each row and column value in the two dimensional array. */
        private static void printArrayData(int array[][])
        {
            // Get row count.
            int rowCount = array.length;
    
            for(int i=0;i<rowCount;i++)
            {
                // Get each row.
                int row[] = array[i];
    
                // Get column count.
                int columnCount = row.length;
    
                for(int j=0;j<columnCount;j++)
                {
                    // Get column value and print.
                    int column = row[j];
                    System.out.print(column);
                    System.out.print(", ");
                }
    
                // Print a return line at the end of each row.
                System.out.println();
            }
        }
    
        /* Swap rows and columns in clockwise direction for two dimensional array. */
        public static int[][] swapArrayClockWise(int array[][])
        {
            return swapArray(array, true);
        }
    
        /* Swap rows and columns in anti-clockwise direction for two dimensional array. */
        public static int[][] swapArrayAntiClockWise(int array[][])
        {
            return swapArray(array, false);
        }
    
        /* Swap rows and columns for two dimensional array. */
        private static int[][] swapArray(int array[][], boolean clockwise)
        {
            int arrayRowCount = getArrayRowCount(array);
    
            int arrayColumnCount = getArrayColumnCount(array);
    
            int newArray[][] = new int[arrayColumnCount][arrayRowCount];
    
            for(int i=0;i<arrayRowCount;i++)
            {
                for(int j=0;j<arrayColumnCount;j++)
                {
                    if(clockwise)
                    {
                        // Swap in clockwise direction.
                        newArray[j][arrayRowCount-i-1] = array[i][j];
                    }else
                    {
                        // Swap in anti-clockwise direction.
                        newArray[j][i] = array[i][j];
                    }
                }
            }
    
            // Return swapped array.
            return newArray;
        }
    
        /* Get array row count. */
        private static int getArrayRowCount(int array[][])
        {
            int ret = 0;
            if(array != null)
            {
                ret = array.length;
            }
    
            return ret;
        }
    
        /* Get the biggest columns count in the array. */
        private static int getArrayColumnCount(int array[][])
        {
            int ret = 0;
            if(array != null)
            {
                int rowCount = array.length;
    
                for(int i=0;i<rowCount;i++)
                {
                    int row[] = array[i];
    
                    if(row.length > ret)
                    {
                        ret = row.length;
                    }
                }
            }
    
            return ret;
        }
    }
  2. Below is the above code execution output.
    Original array data.
    15, 14, 13,
    12, 11, 10,
    7, 8, 9,
    6, 5, 4,
    3, 2, 1,
    
    New array data swapped clockwise.
    3, 6, 7, 12, 15,
    2, 5, 8, 11, 14,
    1, 4, 9, 10, 13,
    
    New array data swapped anti-clockwise.
    15, 12, 7, 6, 3,
    14, 11, 8, 5, 2,
    13, 10, 9, 4, 1

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.