Java Array to String arrayToString(Object[] array)

Here you can find the source of arrayToString(Object[] array)

Description

Converts the specified array to a string.

License

Apache License

Parameter

Parameter Description
array the array to be converted to string. This argument can be null , in which case the string "null" is returned.

Return

the string representation of the passed array. This method never returns null .

Declaration

public static String arrayToString(Object[] array) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    private static final int EXPECTED_ELEMENT_STRING_LENGTH = 32;
    private static final String NULL_STR = "null";

    /**/*from w ww  .  j  a va  2  s  .c o  m*/
     * Converts the specified array to a string. This methods works similar to
     * the {@code java.util.Arrays#toString(Object[])} but for arrays having
     * length of at least 2 returns a multi line string representation where
     * every element has its own dedicated line. This is intended to be more
     * readable for larger arrays.
     *
     * @param array the array to be converted to string. This argument can be
     *   {@code null}, in which case the string "null" is returned.
     * @return the string representation of the passed array. This method never
     *   returns {@code null}.
     *
     * @see #collectionToString(Collection)
     */
    public static String arrayToString(Object[] array) {
        if (array == null) {
            return NULL_STR;
        }

        if (array.length == 0) {
            return "[]";
        } else if (array.length == 1) {
            return "[" + array[0] + "]";
        } else {
            StringBuilder result = new StringBuilder(EXPECTED_ELEMENT_STRING_LENGTH * array.length);

            result.append('[');
            for (int i = 0; i < array.length; i++) {
                result.append('\n');
                result.append(array[i]);
            }
            result.append(']');

            return result.toString();
        }
    }
}

Related

  1. arrayToString(Object[] arr, String split)
  2. arrayToString(Object[] array)
  3. arrayToString(Object[] array)
  4. arrayToString(Object[] array)
  5. arrayToString(Object[] array)
  6. arrayToString(Object[] array)
  7. arrayToString(Object[] array)
  8. arrayToString(Object[] array)
  9. arrayToString(Object[] array)