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.
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:
Reversed 2D Array:
7 8 9
4 5 6
1 2 3Reverse Operation in a 2D Array (elements – position):
This below program helps you to understand to you can reverse each element in 2D array.
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:
Reversed 2D Array:
9 8 7
6 5 4
3 2 1 Key Steps in Reversing a 2D Array:
- Initialize the 2D Array: Declare and initialize the original 2D array.
- Determine Dimensions: Find the number of rows and columns in the array.
- Swap Elements Horizontally: Use nested loops to swap elements horizontally in each row.
- 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:
- Rotate an Array by K Positions (Left and Right)Title: Rotate an Array by K Positions (Left and Right) Array rotation is one of those small, deceptively simple problems… Read more: Rotate an Array by K Positions (Left and Right)
- Reverse an Array in Groups of KTitle: Reverse an Array in Groups of K Reversing an array in groups of K is a classic interview question… Read more: Reverse an Array in Groups of K
- Reverse Half of an ArrayTitle: Reverse Half of an Array In this guide, you’ll learn how to reverse the first half of an integer… Read more: Reverse Half of an Array
- Reverse an Array using RecursionTitle: Reverse an Array using Recursion Reversing an array is one of the first problems every Java developer meets, and… Read more: Reverse an Array using Recursion
- Reverse an Array using a LoopTitle: Reverse an Array using a Loop Reversing an array is one of the first array manipulations every Java developer… Read more: Reverse an Array using a Loop
- Product of Even and Odd Elements in an ArrayTitle: Product of Even and Odd Elements in an Array In this guide, we will solve a classic beginner-friendly array… Read more: Product of Even and Odd Elements in an Array



