×

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

Iterate JSON array in Java

This article is going to equip the knowledge about what JSON array is and how it works and also how is it distinct with the generic array. Json Array Ordered lists of...

4 minutes read.

String Matches in Java

Firstly we have to know something about String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java...

3 minutes read.

Java Comparable Interface

We implement comparable interface when we need a new logic for determining equality. if two objects are equal, the equals() method returns true and compareTo() method returns 0. The basic...

1 minute read.

Java String contains() method

contains() method returns true only if it contains the given sequence of characters otherwise it returns false. syntax: public boolean contains(CharSequence sequence) parameters: sequence : It is the sequence to be searched. Returns: It returns true...

1 minute read.

Bean class in Java

The web applications that are created with the help of the JSP or Java Server Pages generally have fairly more functionality than the web pages that specifically are created with...

5 minutes read.

Java Math log10() Method

The log10() method of Math class returns the base 10 logarithmic value for the specified double argument. Syntax: public static double log10(double a) Parameters: The parameter ‘a’ represents a double value. Return Value: The log10() method...

2 minutes read.

Factorial Program in Java using Recursion

Factorial Program in Java Factorials are used in mathematics to calculate permutations and combinations. It is denoted by the exclamatory symbol (!). Suppose, p is a number whose factorial is to...

3 minutes read.

Java Image

For all other classes used for representing graphical images, Java's Image class serves as an abstract superclass. For images in Java, a specific form of object known as a BufferedImage...

4 minutes read.

Java Vs C++

Java Vs C++ Java and C++ both are Object Oriented Programming languages. Both languages are popular for competitive programming. C++ is used by many coders who have just started learning programming...

4 minutes read.

Java Editors

A straightforward text editor may be used to create Java applications. However, a Java integrated programming environment (IDE) enables the software developer to create programs more quickly. An IDE offers...

4 minutes read.

Implementing Queue Using Array in Java

We can implement queue by using array in Java. The queue is a linear data structure, and the array is one of the simplest data structures. Before implementing a queue...

4 minutes read.

If Condition in Lambda Expression Java

The new and significant lambda expression feature of Java was added in Java SE 8. It provides a clear and concise mechanism for describing a single-method interface using an expression....

4 minutes read.

Java Buffered Writer

BufferWriter Class: It is used to write the data more efficiently. This class is present in the java.io package, it inherits the data from the Writer class. Writer class is...

4 minutes read.

Java DatagramSocket and Java DatagramPacket

Datagrams TCP/IP style networking specifies a serialized, predictable, and reliable stream of data in the form of a packet. Servers and clients communicate through a reliable channel, such as TCP socket, have a dedicated...

6 minutes read.

Type Annotations in Java

Only declarations were eligible for annotations in earlier versions of Java. With Java 8, you may now annotate any type use, including types in declarations, generics, and casts: @Encrypted String data; List<@NonNull...

6 minutes read.

How to create array of objects in Java

Java is an object-oriented programming language therefore everything in Java is based on objects and classes. Array is a data structure that holds data of similar type and dynamically creates...

4 minutes read.

Java inheritance with Example

Java inheritance Java inheritance is a mechanism in which a child object acquires all the properties and behaviors of a parent object. It helps in reusing the code and establishes...

7 minutes read.

Switch Case with Enum in Java

From some conditions, the java switch statement executes one statement. Similar to the If-Else-If ladder statement, this can be Byte, short, int, long, enum, string, and some wrapper types like...

4 minutes read.

How to create a linked list in Java

Introduction: The linked listing is one type of linear statistics shaped like an array. Not like arrays, linked listing factors aren't stored in a contiguous place. The elements have linked...

3 minutes read.

Getter and Setter Method in Java Example

In Java programming, getter and setter methods are often employed. The values of class fields can be accessed and changed using Java's getter and setter methods. A private access specifier...

6 minutes read.