Java Array to String toString(T[] array, String prefix, String sep, String suffix)

Here you can find the source of toString(T[] array, String prefix, String sep, String suffix)

Description

Provides a string representation of the elements in the given array.

License

Open Source License

Parameter

Parameter Description
T The type of array elements.
array An array of elements. It may be null.
prefix String to be used as prefix.
sep String to be used to separate array elements.
suffix String to be used as suffix.

Return

String representation of the elements in the array.

Declaration

public static <T> String toString(T[] array, String prefix, String sep, String suffix) 

Method Source Code

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

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

public class Main {
    /**//from  w w w. j  a va  2s .  c  o  m
     * Provides a string representation of the elements in the given collection.
     *
     * @param c            A collection of elements.  It may be null.
     * @param prefix    String to be used as prefix.
     * @param sep        String to be used to separate elements in the collection.
     * @param suffix    String to be used as suffix.
     * @param <T>        The type of elements in the collection.
     *
     * @return String representation of the elements in the collection.
     */
    public static <T> String toString(Collection<T> c, String prefix, String sep, String suffix) {
        if (c == null || c.size() == 0)
            return prefix + suffix;
        Iterator<T> it = c.iterator();
        String result = prefix + it.next();
        while (it.hasNext())
            result += sep + it.next();
        return result + suffix;
    }

    public static <T> String toString(final Collection<T> a) {
        return toString(a, "", ",", "");
    }

    /**
     * Provides a string representation of the elements in the given array.
     *
     * @param <T>    The type of array elements.
     * @param array  An array of elements.  It may be null.
     * @param prefix String to be used as prefix.
     * @param sep    String to be used to separate array elements.
     * @param suffix String to be used as suffix.
     *
     * @return String representation of the elements in the array.
     */
    public static <T> String toString(T[] array, String prefix, String sep, String suffix) {
        if (array == null || array.length == 0)
            return prefix + suffix;
        String result = prefix + array[0];
        for (int i = 1; i < array.length; i++) {
            result += sep + array[i];
        }
        return result + suffix;
    }

    /**
     * Provides a string representation of the elements in the given array.
     *
     * @param <T>   The type of the array elements.
     * @param array An array of elements.
     *
     * @return string representation of elements in given array.
     */
    public static <T> String toString(final T[] array) {
        return toString(array, "", ",", "");
    }
}

Related

  1. toString(String[] array, String delimiter)
  2. toString(String[] line)
  3. toString(String[] paramNames, Object[] params)
  4. toString(T[] array)
  5. toString(T[] array)
  6. toStringArray(Collection strings)
  7. toStringArray(Dictionary props, String key, String[] defaultArray)
  8. toStringArray(final E[] array)
  9. toStringArray(int[] nums)