Java Array: A Comprehensive Guide 2D Array Reversal

29
java-array-a-comprehensive-guide-1d-array-reversal
Advertisement

Java Array: A Comprehensive Guide 2D Array Reversal

Arrays are the backbone of many algorithms, and mastering their manipulation is key to becoming a proficient Java programmer. In this blog post, we’ll delve into Java Array: A Comprehensive Guide 2D Array Reversal. Follow along for a detailed guide, complete with practical programming examples, and gain insights into the inner workings of the program.

Understanding the Challenge:

Reversing a 2D array involves flipping its elements both horizontally and vertically. This process requires traversing through each row and column to rearrange the elements in reverse order.

Reverse Operation in a 2D Array (row-level):

The Java program for reversing a 2D array is an algorithmic exercise. It employs nested loops to navigate through each element of the array, swapping elements both horizontally and vertically.

Reversal in a 2D Array – Row
public class Reverse2DArray {
    public static void main(String[] args) {
        // Declare and initialize a 2D array
        int[][] matrix = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };

        // Reverse the 2D array
        int rows = matrix.length;
        int columns = matrix[0].length;

        for (int i = 0; i < rows / 2; i++) {
            for (int j = 0; j < columns; j++) {
                // Swap elements horizontally
                int temp = matrix[i][j];
                matrix[i][j] = matrix[rows - i - 1][j];
                matrix[rows - i - 1][j] = temp;
            }
        }

        // Print the reversed 2D array
        System.out.println("Reversed 2D Array:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println(); // Move to the next line after each row
        }
    }
}

Output:

Output – Reversal in a 2D Array – Row Level
Reversed 2D Array:
7 8 9
4 5 6
1 2 3

Reverse Operation in a 2D Array (elements – position):

This below program helps you to understand to you can reverse each element in 2D array.

Reversal in a 2D Array – Element
public class ThreeDArray {
    public static void main(String[] args) {
        // Declare and initialize a 2D array
        int[][] matrix = {
                {1, 2, 3},
                {4, 5, 6},
                {7, 8, 9}
        };

        // Reverse each individual number in the 2D array
        int rows = matrix.length;
        int columns = matrix[0].length;

        for (int i = 0; i < rows/2; i++) {
            for (int j = 0; j < columns; j++) {
                // Swap elements vertically across rows
                int temp = matrix[i][j];
                matrix[i][j] = matrix[rows - i - 1][columns-j-1];
                matrix[rows - i - 1][columns-j-1] = temp;
            }

            for (int k = 0; k < matrix[1].length / 2; k++) {
                // Swap elements from both ends
                int temp = matrix[1][k];
                matrix[1][k] = matrix[1][columns - k - 1];
                matrix[1][columns - i - 1] = temp;
            }
        }



        // Print the reversed 2D array
        System.out.println("Reversed 2D Array:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println(); // Move to the next line after each row
        }
    }
}

Output:

Output – Reversal in a 2D Array – Element
Reversed 2D Array:
9 8 7 
6 5 4 
3 2 1 

Key Steps in Reversing a 2D Array:

  1. Initialize the 2D Array: Declare and initialize the original 2D array.
  2. Determine Dimensions: Find the number of rows and columns in the array.
  3. Swap Elements Horizontally: Use nested loops to swap elements horizontally in each row.
  4. Print Result: Display the reversed 2D array to verify the success of the reversal operation.

Try to work with User Input in Array to make this program more useful while taking user input.

Putting it All Together:

Reversing a 2D array in Java is a crucial skill, especially when working with data tables and matrices. This program provides a foundation for understanding nested array manipulation and serves as a stepping stone to more complex programming challenges.

Experiment with different 2D arrays, observe the results, and solidify your understanding of array manipulation in Java. Happy coding!

Latest posts: