如何在Java中列印下三角矩陣?
在Java中,陣列是一個物件。它是一種非基本資料型別,用於儲存相同資料型別的值。Java中的矩陣只不過是一個多維陣列,它表示多行多列。
三角矩陣 - 如果方陣的對角線以上或以下的所有元素都為零,則該方陣稱為三角矩陣。
下三角矩陣 - 如果方陣的對角線以上的所有元素都為零,則該方陣稱為下三角矩陣。
在這裡,我們必須列印下三角矩陣。
讓我們開始吧!
舉幾個例子
例項 1
給定矩陣 =
21 22 23 24 25 26 27 28 29
給定矩陣的下三角矩陣為 -
21 0 0 24 25 0 27 28 29
例項 2
給定矩陣 =
921 222 243 432 124 745 256 657 237 258 429 345 176 453 756 843
給定矩陣的下三角矩陣為 -
921 0 0 0 124 745 0 0 237 258 429 0 176 453 756 843
例項 3
給定矩陣 =
1 2 3 4 5 6 7 8 9
給定矩陣的下三角矩陣為 -
1 0 0 4 5 0 7 8 9
演算法
演算法 1
步驟 1 - 初始化一個二維陣列矩陣以表示輸入矩陣。
步驟 2 - 使用兩個巢狀迴圈遍歷矩陣的每個元素。外部迴圈遍歷矩陣的行,內部迴圈遍歷每行的列。
步驟 3 - 檢查當前列是否大於當前行。如果是,則將當前元素設定為 0。
步驟 4 - 列印當前元素。
步驟 5 - 內部迴圈完成後,換行以列印下一行。
演算法 2
步驟 1 - 初始化一個二維陣列矩陣以表示輸入矩陣。
步驟 2 - 使用 IntStream 迴圈遍歷矩陣的行。
步驟 3 - 使用另一個 IntStream 迴圈遍歷每行的列。
步驟 4 - 檢查當前列是否大於當前行。如果是,則將當前元素設定為 0。
步驟 5 - 列印當前元素。內部迴圈完成後,換行以列印下一行
語法
1. Java 中的 Matrix.length() 方法返回給定矩陣的長度。
下面是它的語法 -
inputMatrix.lenght
其中,“inputMatrix”指的是給定的矩陣。
2. IntStream.range() 是 Java 中的一種方法,它生成從 startInclusive 到 endExclusive - 1 的順序整數流。
IntStream.range(startInclusive, endExclusive)
它可以用於對一組整數執行操作。例如,在第二個程式碼中,它用於迴圈遍歷矩陣的行和列。
然後在流上呼叫forEach方法,以對流中的每個元素執行操作。
多種方法
我們提供了不同方法的解決方案。
使用巢狀迴圈
使用流
讓我們逐一檢視程式及其輸出。
方法 1:使用巢狀迴圈
在這種方法中,矩陣元素將在程式中初始化。然後透過將矩陣作為引數傳遞來呼叫使用者定義的方法,並在方法內部根據演算法使用巢狀迴圈方法列印給定矩陣的下三角矩陣。
示例
public class Main {
public static void main(String[] args) {
int[][] inputMatrix = {{11, 22, 33, 44},
{55, 66, 77, 88},
{99, 10, 11, 12},
{13, 14, 15, 16}};
LowerTriangularMatrix(inputMatrix);
}
public static void LowerTriangularMatrix(int[][] mat) {
System.out.println("Lower triangular matrix of given matrix is: ");
// initiate the loop to check through the rows
for (int a = 0; a < mat.length; a++) {
// then check through each columns
for (int b = 0; b < mat[a].length; b++) {
// If the column is greater than the row, set the element to 0
if (b > a) {
mat[a][b] = 0;
}
// Print the current element
System.out.print(mat[a][b] + " ");
}
System.out.println();
}
}
}
輸出
Lower triangular matrix of given matrix is: 11 0 0 0 55 66 0 0 99 10 11 0 13 14 15 16
方法 2:使用流
在這種方法中,矩陣元素將在程式中初始化。然後透過將矩陣作為引數傳遞來呼叫使用者定義的方法,並在方法內部根據演算法使用流方法列印給定矩陣的下三角矩陣。
示例
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
int[][] inputMatrix = {{12, 32, 13},
{5, 6, 7},
{9, 10, 11}};
//call the user-defined method
lowerTriangularMatrix(inputMatrix);
}
public static void lowerTriangularMatrix(int[][] mat) {
System.out.println("Lower triangular matrix of given matrix is: ");
// Use IntStream to loop through the rows of the matrix
IntStream.range(0, mat.length)
.forEach(a -> {
// Use IntStream to loop through the columns of the current row
IntStream.range(0, mat[a].length)
.forEach(b -> {
// If the column is greater than the row, set the element to 0
if (b > a) {
mat[a][b] = 0;
}
// Print the current element
System.out.print(mat[a][b] + " ");
});
System.out.println();
});
}
}
輸出
Lower triangular matrix of given matrix is: 12 0 0 5 6 0 9 10 11
在這篇文章中,我們探索了使用 Java 程式語言列印下三角矩陣的不同方法。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP