print Matrix Diagonals - Java java.lang

Java examples for java.lang:Math Matrix

Description

print Matrix Diagonals

Demo Code


//package com.java2s;

public class Main {
    static void printMatrixDiagonals(int[][] a) { // print SW-NE
        for (int k = 0; k < a.length * 2; k++) {
            for (int j = 0; j <= k; j++) {
                int i = k - j;
                if (i < a.length && j < a[0].length)
                    System.out.print(a[i][j] + " ");
            }//w  w  w.  j  av a  2s. c o  m
            System.out.println();
        }
    }

    static void print(int[] arr) {
        for (int i = 0; i < arr.length; i++)
            System.out.print(arr[i] + " ");
        System.out.println();
    }

    static void print(int[][] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println();
    }
}

Related Tutorials