Here you can find the source of join(final ArrayList
private static String join(final ArrayList<String> parts, final char separator)
//package com.java2s; import java.util.ArrayList; public class Main { private static String join(final ArrayList<String> parts, final char separator) { final int size = parts.size(); /* Calculating the size of the resulting string to avoid redundant buffer re-allocations. * /*w ww. j a v a 2 s . com*/ * destSize is initialised with the number of separators in the resulting string. */ int destSize = size - 1; for (int i = 0; i < size; ++i) { destSize += parts.get(i).length(); } // The buffer is created with the necessary capacity. final StringBuilder buf = new StringBuilder(destSize); int i = 0; for (final int n = size - 1; i < n; ++i) { buf.append(parts.get(i)).append(separator); } buf.append(parts.get(i)); assert buf.length() == destSize; return buf.toString(); } }