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:
- Minimum Number of Operations to Make Array EqualTitle: Minimum Number of Operations to Make Array Equal In this guide, you’ll learn how to solve the Minimum Number… Read more: Minimum Number of Operations to Make Array Equal
- Minimum Number of Deletions to Make an Array SortedTitle: Minimum Number of Deletions to Make an Array Sorted Sorting problems often hide a subtler question: what is the… Read more: Minimum Number of Deletions to Make an Array Sorted
- Minimum Number of Swaps to Bring All Elements Less Than K TogetherTitle: Minimum Number of Swaps to Bring All Elements Less Than K Together In this guide, you’ll learn how to… Read more: Minimum Number of Swaps to Bring All Elements Less Than K Together
- Minimum Number of Swaps to Sort an ArrayTitle: Minimum Number of Swaps to Sort an Array In this guide, you’ll learn how to find the minimum number… Read more: Minimum Number of Swaps to Sort an Array
- Split Array into Three Equal Sum PartsTitle: Split Array into Three Equal Sum Parts Splitting an array into three parts with equal sums is a classic… Read more: Split Array into Three Equal Sum Parts
- Partition Array into K Equal Sum SubsetsTitle: Partition Array into K Equal Sum Subsets In this guide, you’ll learn how to solve the Partition Array into… Read more: Partition Array into K Equal Sum Subsets



