如何在 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 - 使用巢狀 for 迴圈將矩陣旋轉 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 度的不同方法。

更新於: 2023 年 3 月 9 日

654 次檢視

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告