Java Array to String arrayToString(Object[] array)

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

Description

Returns a comma-delimited string representation of the specified array, with []s.

License

Open Source License

Declaration

public static String arrayToString(Object[] array) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from   ww w . j av a  2  s .co  m*/
     * Returns a comma-delimited string representation of the specified array, with []s.
     */
    public static String arrayToString(Object[] array) {
        return arrayToString(array, ", ", -1);
    }

    /**
     * Returns a delimited string representation of the specified array, with []s.
     * @param delim what delimiter to use in between array elements.
     */
    public static String arrayToString(Object[] array, String delim) {
        return arrayToString(array, delim, -1);
    }

    /**
     * @param delim what delimiter to use in between array elements.
     * @param maxValues how many array elements to include.
     *      When less than 0, all values are included.
     *      If <code>maxValues</code> is greater than the number of elements in <code>array</code>,
     *      then all elements are included.
     *      If any elements are not included, <code>...</code> will be inserted after the last element.
     */
    public static String arrayToString(Object[] array, String delim, int maxValues) {
        if (delim == null)
            throw new IllegalArgumentException();
        if (array == null)
            return null;
        if (array.length == 0)
            return "";

        int max = maxValues < 0 ? array.length : Math.min(array.length, maxValues);
        StringBuffer temp = new StringBuffer(2 + (max * 16));

        for (int i = 0; i < max; i++) {
            if (i > 0)
                temp.append(delim);
            temp.append(array[i]);
        }
        if (max < array.length)
            temp.append("...");

        return temp.toString();
    }
}

Related

  1. arrayToString(Object[] array)
  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)