Java 将矩阵旋转180度

Java 将矩阵旋转180度

在Java中,数组是一个对象。它是一种非原始数据类型,用于存储相似数据类型的值。在Java中,矩阵实际上就是一个多维数组,表示多行和多列。

根据问题的陈述,我们需要将给定的矩阵旋转180度。这意味着我们需要在对称垂直方向上交换给定矩阵的行。

让我们深入研究这篇文章,以了解如何使用Java编程语言实现。

给您展示一些示例

示例1

假设原始矩阵为:

{
   {10, 20, 30},
   {40, 50, 60},
   {70, 80, 90}
}

将矩阵旋转180度后:

{
   {90, 80, 70},
   {60, 50, 40},
   {30, 20, 10}
}

示例2

假设原始矩阵为

{
   {1, 2, 3},
   {4, 5, 6},
   {7, 8, 9}
}

矩阵旋转180度后:

{
   {9, 8, 7},
   {6, 5, 4},
   {3, 2, 1}
}

示例3

假设原始矩阵如下:

{
   {11, 22, 33},
   {44, 55, 66},
   {77, 88, 99}
}

将矩阵旋转180度后:

{
   {99, 88, 77},
   {66, 55, 44},
   {33, 22, 11}
}

步骤

步骤1 - 声明并初始化一个整数类型的多维数组。

步骤2 - 声明两个整数类型的变量,用于存储给定矩阵的行数和列数。

步骤3 - 使用嵌套循环将矩阵旋转180度,并将新的矩阵存储到另一个空矩阵中。

步骤4 - 将结果矩阵打印输出。

语法

要获取数组的长度(数组中的元素数),数组有一个内置的属性,即 length

以下是它的语法参考

array.length

‘array’是指数组引用。

多种方法

我们以不同的方法提供了解决方案。

  • 通过使用数组元素的静态初始化

  • 通过使用用户定义的方法

让我们逐一查看程序及其输出。

方法1:通过使用pow()函数对矩阵进行静态初始化

在这种方法中,矩阵元素将在程序中初始化。然后按照算法的要求,将矩阵元素替换为它的平方。这里我们将使用内置的pow()函数来获取元素的平方。

示例

public class Main{
   public static void main(String[] args){

      //declare a matrix with some random values
      int[][] inputMatrix = { 
         {10, 20, 30},
         {40, 50, 60},
         {70, 80, 90}
      };

      //declare a variable to store the row count
      int r = inputMatrix.length;

      //declare a variable to store the row count
      int c = inputMatrix[0].length;

      //declare an empty matrix
      int[][] rotatedMAt = new int[r][c];

      //take nested for loop to rotate the matrix to 180 degree
      for (int i = 0; i < r; i++){
         for (int j = 0; j < c; j++){
            rotatedMAt[i][j] = inputMatrix[r - i - 1][c - j - 1];
         }
      }

      //print the given matrix
      System.out.println("Given Matrix:");
      for (int i = 0; i < r; i++){
         for (int j = 0; j < c; j++){
            System.out.print(inputMatrix[i][j] + " ");
         }
         System.out.println();
      }

      //print the rotated matrix
      System.out.println("Rotated- 180 degree Matrix:");
      for (int i = 0; i < r; i++){
         for (int j = 0; j < c; j++){
            System.out.print(rotatedMAt[i][j] + " ");
         }
         System.out.println();
      }
   }
}

输出

Given Matrix:
10 20 30 
40 50 60 
70 80 90 
Rotated- 180 degree Matrix:
90 80 70 
60 50 40 
30 20 10

方法2:使用用户自定义的方法

在这种方法中,数组元素将在程序中进行初始化。然后通过将数组作为参数来调用一个用户定义的方法,并在方法内根据算法将矩阵旋转180度。

示例

public class Main{

   //user-defined method to rotate the matrix to 180 degree
   public static void Rotate(int[][] inputMatrix){

      //declare a variable to store the row count
      int r = inputMatrix.length;

      //declare a variable to store the row count
      int c = inputMatrix[0].length;

      //declare an empty matrix
      int[][] rotatedMAt = new int[r][c];

      //take nested for loop to rotate the matrix to 180 degree
      for (int i = 0; i < r; i++){
         for (int j = 0; j < c; j++){
            rotatedMAt[i][j] = inputMatrix[r - i - 1][c - j - 1];
         }
      }

      //print the given matrix
      System.out.println("Given Matrix:");
      for (int i = 0; i < r; i++){
         for (int j = 0; j < c; j++){
            System.out.print(inputMatrix[i][j] + " ");
         }
         System.out.println();
      }

      //print the rotated matrix
      System.out.println("Rotated- 180 degree Matrix:");
      for (int i = 0; i < r; i++){
         for (int j = 0; j < c; j++){
            System.out.print(rotatedMAt[i][j] + " ");
         }
         System.out.println();
      }
   }
   public static void main(String[] args){

      //declare a matrix with some random values
      int[][] inpMatrix = {
         {22, 12, 54},
         {2, 76, 23},
         {124, 67, 34}
      };

      //call the user-defined method
      Rotate(inpMatrix);
   }
}

输出

Given Matrix:
22 12 54 
2 76 23 
124 67 34 
Rotated- 180 degree Matrix:
34 67 124 
23 76 2 
54 12 22

在本文中,我们通过使用Java编程语言探索了不同的方法来将矩阵旋转180度。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程