Java Array Output printArray(final Object[] array, int indent)

Here you can find the source of printArray(final Object[] array, int indent)

Description

utility method, prints out an array using the toString() method of the members.

License

Open Source License

Parameter

Parameter Description
array can be null
indent the level of indents (tabs) that precede the output, can be negative

Declaration

public static void printArray(final Object[] array, int indent) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from   w  w  w. j  a v  a2  s . c o m
     * utility method, prints out an array using the toString() method of the members.
     * @param array can be null
     * @param indent the level of indents (tabs) that precede the output, can be negative
     */
    public static void printArray(final Object[] array, int indent) {
        System.out.println(toString(array, indent, false, true));
    }

    /**
     * utility method, transforms the array into a String that can either be multi-line or
     * on one line.
     *
     * @param array the array to be transformed, can be null
     * @param indent the number of tabs to be used for indenting
     * @param oneLine if true, a String containing a single line is returned, if false, return characters
     * ("\n") are integrated into the returned string.
     * @param numbered if true, each element is preceded by its index in the array
     * @return a string with the array contents, formated as requested
     */
    public static String toString(final Object[] array, int indent, boolean oneLine, boolean numbered) {

        if (array == null) {
            return "(null)";
        }

        StringBuffer sb = new StringBuffer();
        for (int jx = 0; jx < indent; jx++) {
            sb.append('\t');
        }

        for (int ix = 0; ix < array.length; ix++) {
            if (numbered) {
                sb.append('[');
                sb.append(ix);
                sb.append("]: ");
            }
            sb.append(array[ix]);
            if (oneLine) {
                if (ix < array.length - 1) { // don't append to the last one
                    sb.append(", ");
                } // no else here
            } else {
                sb.append("\n");
                for (int jx = 0; jx < indent; jx++) {
                    sb.append('\t');
                }
            }
        }
        return sb.toString();
    }
}

Related

  1. printArray(double[] array)
  2. printArray(double[] array, String msgBefore, String msgAfter)
  3. printArray(double[][] in)
  4. printArray(double[][] in)
  5. printarray(final int[] a)
  6. printArray(float[] arr)
  7. printArray(int arr[])
  8. printArray(int[] arr)
  9. printArray(int[] array)