Here you can find the source of print(Iterable
Parameter | Description |
---|---|
coll | Set of items to be printed |
sep | The separator to add between each elements. |
T | a parameter |
public static <T> String print(Iterable<T> coll, String sep)
//package com.java2s; //License from project: Open Source License import java.util.*; public class Main { /**/*from ww w . j av a 2 s.co m*/ * Prints all items in a collection to a string, separated by a custom separator. * @param coll Set of items to be printed * @param sep The separator to add between each elements. * @param <T> * @return A string representation of the collection. */ public static <T> String print(Iterable<T> coll, String sep) { StringBuilder builder = new StringBuilder(); Iterator<T> it = coll.iterator(); while (it.hasNext()) { T item = it.next(); builder.append(item.toString()); if (it.hasNext()) builder.append(sep); } return builder.toString(); } /** * Prints all items in an array to a string, separated by a custom separator. * @param coll Set of items to be printed * @param sep The separator to add between each elements. * @param <T> * @return A string representation of the collection. */ public static <T> String print(T[] coll, String sep) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < coll.length; i++) { builder.append(coll[i]); if (i < coll.length - 1) builder.append(sep); } return builder.toString(); } }