Java Array Output printArray(double[] array, String msgBefore, String msgAfter)

Here you can find the source of printArray(double[] array, String msgBefore, String msgAfter)

Description

print Array

License

Open Source License

Declaration

public static void printArray(double[] array, String msgBefore, String msgAfter) 

Method Source Code

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

public class Main {
    public static void printArray(double[] array, String msgBefore, String msgAfter) {
        System.out.print(msgBefore);
        System.out.print(arrayToString(array));
        System.out.print(msgAfter);
    }//from   w w  w  . j  av a 2 s .com

    public static void printArray(int[] array, String msgBefore, String msgAfter) {
        System.out.print(msgBefore);
        System.out.print(arrayToString(array));
        System.out.print(msgAfter);
    }

    public static String arrayToString(int[] array) {
        StringBuilder output = new StringBuilder();
        for (int i = 0; i < array.length - 1; i++) {
            output.append(String.format("%d\t", array[i]));
        }
        output.append(String.format("%d", array[array.length - 1]));
        return output.toString();
    }

    public static String arrayToString(double[] array) {
        StringBuilder output = new StringBuilder();
        for (int i = 0; i < array.length - 1; i++) {
            output.append(String.format("%.2f\t", array[i]));
        }
        output.append(String.format("%.2f", array[array.length - 1]));
        return output.toString();
    }

    public static String arrayToString(double[] array, String format) {
        StringBuilder output = new StringBuilder();
        for (int i = 0; i < array.length - 1; i++) {
            output.append(String.format(format + "\t", array[i]));
        }
        output.append(String.format(format, array[array.length - 1]));
        return output.toString();
    }

    public static String arrayToString(double[] array, int digit) {
        StringBuilder output = new StringBuilder();
        for (int i = 0; i < array.length - 1; i++) {
            output.append(String.format("%." + digit + "f\t", array[i]));
        }
        output.append(String.format("%." + digit + "f", array[array.length - 1]));
        return output.toString();
    }
}

Related

  1. printArray(char[] array)
  2. printArray(double[] array)
  3. printArray(double[][] in)
  4. printArray(double[][] in)
  5. printarray(final int[] a)
  6. printArray(final Object[] array, int indent)