Here you can find the source of join(Object[] array, String separator)
public static String join(Object[] array, String separator)
//package com.java2s; //License from project: Apache License import java.util.Iterator; public class Main { public static String join(Iterator<?> iterator, char separator) { // handle null, zero and one elements before building a buffer if (iterator == null) { return null; }/*w ww .ja va2 s . co m*/ if (!iterator.hasNext()) { return ""; } Object first = iterator.next(); if (!iterator.hasNext()) { return first == null ? "" : first.toString(); } // two or more elements StringBuilder buf = new StringBuilder(256); // Java default is 16, probably too small if (first != null) { buf.append(first); } while (iterator.hasNext()) { buf.append(separator); Object obj = iterator.next(); if (obj != null) { buf.append(obj); } } return buf.toString(); } public static String join(Iterator<?> iterator, String separator) { // handle null, zero and one elements before building a buffer if (iterator == null) { return null; } if (!iterator.hasNext()) { return ""; } Object first = iterator.next(); if (!iterator.hasNext()) { return first == null ? "" : first.toString(); } // two or more elements StringBuilder buf = new StringBuilder(256); // Java default is 16, probably too small if (first != null) { buf.append(first); } while (iterator.hasNext()) { if (separator != null) { buf.append(separator); } Object obj = iterator.next(); if (obj != null) { buf.append(obj); } } return buf.toString(); } public static String join(Iterable<?> iterable, char separator) { if (iterable == null) { return null; } return join(iterable.iterator(), separator); } public static String join(Iterable<?> iterable, String separator) { if (iterable == null) { return null; } return join(iterable.iterator(), separator); } public static String join(Object[] array, String separator, int startIndex, int endIndex) { if (array == null) { return null; } if (separator == null) { separator = ""; } // endIndex - startIndex > 0: Len = NofStrings *(len(firstString) + len(separator)) // (Assuming that all Strings are roughly equally long) int noOfItems = endIndex - startIndex; if (noOfItems <= 0) { return ""; } StringBuilder buf = new StringBuilder(noOfItems * 16); for (int i = startIndex; i < endIndex; i++) { if (i > startIndex) { buf.append(separator); } if (array[i] != null) { buf.append(array[i]); } } return buf.toString(); } public static String join(Object[] array, String separator) { if (array == null) { return null; } return join(array, separator, 0, array.length); } public static String join(Object[] array, char separator, int startIndex, int endIndex) { if (array == null) { return null; } int noOfItems = endIndex - startIndex; if (noOfItems <= 0) { return ""; } StringBuilder buf = new StringBuilder(noOfItems * 16); for (int i = startIndex; i < endIndex; i++) { if (i > startIndex) { buf.append(separator); } if (array[i] != null) { buf.append(array[i]); } } return buf.toString(); } }