Java ArrayList Join join(final ArrayList parts, final char separator)

Here you can find the source of join(final ArrayList parts, final char separator)

Description

join

License

Open Source License

Declaration

private static String join(final ArrayList<String> parts, final char separator) 

Method Source Code

//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();
    }
}

Related

  1. join(ArrayList nameList, String delimiter)
  2. join(ArrayList s, String delimiter)
  3. join(ArrayList strings, String separator)
  4. join(final ArrayList array, final String separator)
  5. join(final ArrayList arry, final String with)
  6. join(String glue, ArrayList a)
  7. joinArrayList(List coll, String delimiter)
  8. joinArrayList(String joiner, ArrayList array)
  9. joinCommandLine(ArrayList lsCmdl)