Java Array Output printArray(double[] array)

Here you can find the source of printArray(double[] array)

Description

print Array

License

BSD License

Parameter

Parameter Description
array a parameter

Declaration

public static void printArray(double[] array) 

Method Source Code

//package com.java2s;
/**/*from  w w  w  .ja v a2s .c o m*/
 * <p>
 * This software is distributed under the <a href="http://hci.stanford.edu/research/copyright.txt">
 * BSD License</a>.
 * </p>
 * 
 * @author <a href="http://graphics.stanford.edu/~ronyeh">Ron B Yeh</a> (ronyeh(AT)cs.stanford.edu)
 */

public class Main {
    /**
     * @param array
     */
    public static void printArray(double[] array) {
        System.out.print("Double Array: [");
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]);
            if (i != array.length - 1) {
                System.out.print(", ");
            }
        }
        System.out.println("]");
    }

    /**
     * Prints an array of Objects to console.
     * 
     * @param array
     */
    public static void printArray(Object[] array) {
        String className = array[0].getClass().toString();
        System.out.print(className.substring(className.lastIndexOf(".") + 1, className.length()) + " Array: [");
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]);
            if (i != array.length - 1) {
                System.out.print(", ");
            }
        }
        System.out.println("]");
    }

    /**
     * @param array
     *            turns an array of ints into a String
     */
    public static String toString(int[] array) {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (int i : array) {
            sb.append(i + ", ");
        }
        if (sb.length() > 1) {
            sb.delete(sb.length() - 2, sb.length());
        }
        sb.append("]");
        return sb.toString();
    }

    /**
     * @param array
     * @return
     */
    public static String toString(Object[] array) {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (Object o : array) {
            sb.append(o + ", ");
        }
        if (sb.length() > 1) {
            sb.delete(sb.length() - 2, sb.length());
        }
        sb.append("]");
        return sb.toString();
    }
}

Related

  1. printArray(char[] array)
  2. printArray(double[] array, String msgBefore, String msgAfter)
  3. printArray(double[][] in)
  4. printArray(double[][] in)
  5. printarray(final int[] a)