×

How to create a mirror image of a 2D array in Java

Problem Statement

We have provided a list of m x n. here m indicates rows, and n indicates columns). Printing the fresh matrices should result in a mirror reflection of an old matrix.

Inverted image

The first and final columns' items are switched around in a two-dimensional array of m*n that is mirrored. According to the adjacent graphic, the central column is fixed. Keep in mind that the only modified rows are the columns of a mirror copy.

The 2nd and 4th columns will be switched if the matrix is 4*4.

Note:

The center columns would be constant, and the other columns would be switched if several such columns inside the provided matrix were odd.

How to Create a Mirror Image of A 2D Array in Java?

Java Program for finding the mirror image of the array

Example: MirrorImages.java

// this program is for finding the mirror image of the given array
//importing the required packages
import java.io.*;
import java.util.*;
import java.util.Scanner;  
public class MirrorImages  
{  
public static void main(String args[])   
{  
//establishing a new scanner instance
Scanner sc = new Scanner(System.in);  
System.out.print("Please enter the rows(r): ");  
// reading the input as rows 
int r = sc.nextInt();  
System.out.print(" Please enter the number of the columns(c): ");  
// reading the input as columns  
int c = sc.nextInt();  
// an array was created consisting of m*n order
int array[][] = new int[r][c];  
// a new array for storing the result 
int newArray[][] = new int[r][c];  
System.out.println("Please enter the elements of the array");  
// reading the array elements  
for (int i = 0; i < r; i++)   
{  
System.out.println("Enter thr rows "+ (i+1) + ":");  
for (int j = 0; j < c; j++)   
{  
array[i][j] = sc.nextInt();  
}  
}  
// displaying the input array
System.out.println("The user input array is:");  
for (int i = 0; i < r; i++)   
{  
for (int j = 0; j < c; j++)   
{  
System.out.print(array[i][j] + "\t");  
}  
System.out.println();  
}  
// the elements of the array are exchanging
for (int j = 0; j < c; j++)   
{  
for (int i = 0; i < r; i++)   
{  
newArray[i][c - 1 - j] = array[i][j];   
}  
}  
// displaying the out as the mirror image
System.out.println("Mirror Image of the Given Array is:");  
for (int i = 0; i < r; i++)   
{  
for (int j = 0; j < c; j++)   
{  
System.out.print(newArray[i][j] + "\t");  
}  
System.out.println();  
}  
}  
}

Output

Please enter the rows(r): 3
Please enter the number of the columns(c): 3
Please enter the elements of the arrayEnter the rows 1:
1 2 3
Enter thr rows 2:
4 5 6
Enter thr rows 3:
7 8 9
The user input array is:1
1 2 3
4 5 6
7 8 9
Mirror Image of the Given Array is:
3 2 1
6 5 4
9 8 7

Related Topics

Java Variable Declaration

In this article, you will be acknowledged about java variable declaration. You will be able to learn and interpret about declaring a variable in Java. Variable in Java Variables are necessary for...

6 minutes read.

Special Operators in Java

An operator in Java is a special symbol. It is used to perform operations on two or more variables.  We all know basic operations in Java. There are 8 types...

5 minutes read.

Display Unique Rows in a Binary Matrix in Java

To solve this problem, we must first locate and display the distinct rows of a supplied binary matrix afterward. We will go through how to show distinct rows inside a...

12 minutes read.

Trimorphic numbers in Java

Wondered what the Trimorphic number is!! In this article, you can learn about what a Trimorphic number is referred to as and how to find whether a number is trimorphic...

3 minutes read.

Read JSON file in Java

 To know about reading a JSON file in Java first we must know about the JSON file. JSON: JSON is “JavaScript Object Notation”. The JSON is the simple format for storing and...

3 minutes read.

How to Convert int to long in Java

How to Convert int to long in Java When two variables of different types are involved in the single expression, Java compiler uses built-in library function to convert the variable to...

2 minutes read.

Java vs JavaScript

Java Vs JavaScript Java and JavaScript, both play an important role in the field of Computer Technology. Most people think JavaScript is a part of Java. But it is not entirely...

4 minutes read.

Minimum Difference Between Groups of Size Two in Java

There is given an array with various integers in it. The goal is to divide the elements into distinct groups, each of which has just two, so that the difference...

3 minutes read.

Minimum Number of Taps to Open to Water a Garden in Java

Problem Statement The issue is that a gardener wants to water a (single-dimensional) garden using the fewest possible tap openings. The goal is to determine the minimum necessary taps to be...

8 minutes read.

Java Integer highestOneBit()

The highestOneBit() method of Java Integer class returns an int value with at most a single one-bit, in the position of the highest-order one-bit in the specified int value.  Syntax public static...

1 minute read.

Java String indexOf() method

Java String indexOf() method returns index of a given character or substring present in a String. Methods Description int indexOf(int ch)     It returns index position for the given char value.int indexOf(int ch,...

2 minutes read.

What is new in Java 17?

Java 17 LTS is the most recent long-term support release for the Java SE platform. Under the Oracle No-Fee Terms and Conditions License, which stands for long-term support, JDK 17...

6 minutes read.

Java Break Keyword

Break: The word Break is a keyword or a statement in a java programming language.This Keyword break is used to stop the execution of the loop or switch statements etc.The loop...

3 minutes read.

Ternary Operator in Java

In some cases, the if...else expression in Java can be replaced by a ternary operator. Visit the Java if...else statement first before learning about the ternary operator. Ternary operator in Java A...

3 minutes read.

Class memory in Java

Anything specifically defined in the Java code is stored either inside the heap or the stack, which is particularly significant. Stack and heap are the two kinds of memory available...

6 minutes read.

CopyOnWriteArrayList in Java

The CopyOnWriteArrayList is used to implement the List Interface. It is the improved version of ArrayList. The operations like add, remove, set, update etc, these operations are done by creating...

5 minutes read.

How to Split the String in Java with Delimiter

In Java, splitting strings is a significant and typically used activity while coding. Java gives different ways of dividing the String. The most widely recognized way is to use the...

3 minutes read.

Java.net.ConnectionException

java.net.ConnectException: Connection rejected: the interface is the most continuous sort of happening, organizing special cases in Java at whatever point the product is in client-server engineering and attempting to make...

3 minutes read.

Java Snippets

The term "snippet" refers to a section of code that addresses numerous issues with just a few lines of code. Decreases the number of lines of code and improves programmer...

3 minutes read.

Operators in Java

There is a good operator environment provided by Java. These operators are divided into four different groups i.e. arithmetic, bitwise, relational, and logical. There are other operators also available to...

7 minutes read.