java - 对角线上的矩阵元素

标签 java arrays matrix 2d

public class Main {
    public static void main(String[] args) {
        int array[][] = {
                {2, 3, 4, 5, 6},
                {2, 3, 4, 5, 6},
                {2, 3, 4, 5, 6},
                {2, 3, 4, 5, 6},
                {2, 3, 4, 5, 6},
        };
        int i, j;
        {
            for (i = 0; i <5; i++) {
                for (j = 0+i; j < 5; j++) {
                    System.out.print(array[i][j] + " ");


                }
                System.out.println();
            }
        }
    }
}

参见:example image

我如何给出高对角线的美学观点? 换行后需要换行。

最佳答案

例如:

for (i = 0; i <5; i++) {
    for (j = 0; j < 5; j++) {
        if( j >= i ) {
            System.out.printf( "   ");
        else {
            System.out.printf( "%3d", array[i][j] );
        }
     }
     System.out.println();
}

关于java - 对角线上的矩阵元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35522920/

相关文章:

java - 根据 Yaml 架构验证 JSON 数据

java - AXIS 2 Web 服务访问中的 ClassCastException : RequestURIBasedDispatcher

javascript - 使用 Jackson ObjectMapper 将 Java HashMap 序列化为 JSON

c - 何时在指针分配中使用*?

java - java中向数组添加变量元素

python - 保存一个 numpy 矩阵

java - Java 16 位字符如何支持 Unicode?

c - 树莓派上的结构段错误gcc

javascript - 如何快速缩放javascript中的二维数组?

java - java中如何只抓取二维矩阵的某一行?