Java Array to String arrayToString(final T[] array)

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

Description

array To String

License

Open Source License

Declaration

public static <T> String arrayToString(final T[] array) 

Method Source Code

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

import java.io.StringWriter;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;

public class Main {
    private static final String DEFAULT_LIST_SEPARATOR = ", ";

    public static <T> String arrayToString(final T[] array) {
        return collectionToString(Arrays.asList(array), DEFAULT_LIST_SEPARATOR);
    }/*  w  w w. j a  v a  2 s .  c  o m*/

    public static <T> String arrayToString(final T[] array, final String separator) {
        return collectionToString(Arrays.asList(array), separator);
    }

    public static String collectionToString(final Collection<?> coll) {
        return collectionToString(coll, DEFAULT_LIST_SEPARATOR);
    }

    public static String collectionToString(final Collection<?> coll, final String separator) {
        StringWriter sw = new StringWriter();
        Iterator<?> it = coll.iterator();
        boolean first = true;
        while (it.hasNext()) {
            if (first) {
                first = false;
            } else {
                sw.append(separator);
            }
            Object obj = it.next();
            sw.append(obj == null ? "null" : obj.toString());
        }
        return sw.toString();
    }
}

Related

  1. arrayToString(final String[] args)
  2. arrayToString(final String[] strs)
  3. arrayToString(final String[] value)
  4. arrayToString(final String[] values)
  5. arrayToString(final T... data)
  6. arrayToString(float[] a)
  7. arrayToString(float[] arg)
  8. arrayToString(float[][] array)
  9. arrayToString(int[] ar)