Java Matrix Print print2DIntArray(int[][] array)

Here you can find the source of print2DIntArray(int[][] array)

Description

print D Int Array

License

Open Source License

Declaration

public static void print2DIntArray(int[][] array) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Arrays;

public class Main {
    public static void print2DIntArray(int[][] array) {
        print2DArray(getBoxedInt2DArray(array));
    }//from   w  w  w  .  jav  a  2s.c o  m

    public static <T> void print2DArray(T[][] array) {
        if (array == null) {
            return;
        }
        int rows = array.length;
        System.out.println("[");
        for (int i = 0; i < rows; i++) {
            printArray(array[i]);
        }
        System.out.println("]");
    }

    private static Object[][] getBoxedInt2DArray(int[][] nums) {
        if (nums == null) {
            return null;
        }

        Object[][] result = new Object[nums.length][nums[0].length];
        for (int i = 0; i < nums.length; i++) {
            result[i] = getBoxedIntArray(nums[i]);
        }

        return result;
    }

    public static <T> void printArray(T[] array) {
        int len = array.length;
        System.out.print("[");
        for (int i = 0; i < len; i++) {
            if (i != len - 1) {
                System.out.print(array[i] + ", ");
            } else {
                System.out.println(array[i] + "]");
            }
        }
    }

    public static Object[] getBoxedIntArray(int[] nums) {
        if (nums == null) {
            return null;
        }
        return Arrays.stream(nums).boxed().toArray();
    }
}

Related

  1. print2DIntArray(int[][] nums)
  2. printMatrix(boolean[][] matrix)
  3. printMatrix(byte[][] matrix)
  4. printMatrix(byte[][] matrix, boolean printMatrix)