如何在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