Here you can find the source of print(Collection
Parameter | Description |
---|---|
elements | a parameter |
public static <T> String print(Collection<T> elements)
//package com.java2s; //License from project: Open Source License import java.util.*; public class Main { /**//from ww w . j a va2 s .c om * Returns a String representation of given elements, separated by comma and surrounded by * square brackets. * @param elements * @return a String */ public static <T> String print(T[] elements) { return print(elements, ",", "[", "]"); } /** * Returns a String representation of given elements, with given separator and not surrounded by * any other characters. * @param elements * @param separator * @return a String */ public static <T> String print(T[] elements, String separator) { return print(elements, separator, "", ""); } /** * Returns a String representation of given elements, using given separator and surrounding * strings. * @param elements the elements to print, they are printed in the order they were specified * @param separator the separator string * @param begin the prefix string, appended at first * @param end the postfix string, appended at last * @return a String */ public static <T> String print(T[] elements, String separator, String begin, String end) { StringBuilder ret = new StringBuilder(); if (elements.length == 0) { ret.append(begin).append(end); return ret.toString(); } else { ret.append(begin); boolean first = true; for (T element : elements) if (first) { ret.append(element); first = false; } else ret.append(separator).append(element); ret.append(end); return ret.toString(); } } /** * Returns a String representation of given collection, separated by comma and surrounded by * square brackets. * @param elements * @return a String */ public static <T> String print(Collection<T> elements) { return print(elements, ",", "[", "]"); } /** * Returns a String representation of given collection, with given separator and not surrounded * by any other characters. * @param elements * @param separator * @return a String */ public static <T> String print(Collection<T> elements, String separator) { return print(elements, separator, "", ""); } /** * Returns a String representation of given collection, using given separator and surrounding * strings. * @param elements the elements to print, they are printed in the order they were specified * @param separator the separator string * @param begin the prefix string, appended at first * @param end the postfix string, appended at last * @return a String */ public static <T> String print(Collection<T> elements, String separator, String begin, String end) { StringBuilder ret = new StringBuilder(); if (elements.isEmpty()) { ret.append(begin).append(end); return ret.toString(); } else { ret.append(begin); boolean first = true; for (T element : elements) if (first) { ret.append(element); first = false; } else ret.append(separator).append(element); ret.append(end); return ret.toString(); } } }