Java Array Join join(final Object[] array, final char separator)

Here you can find the source of join(final Object[] array, final char separator)

Description

join

License

Apache License

Declaration

public static String join(final Object[] array, final char separator) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Iterator;

public class Main {
    public static final String EMPTY = "";

    public static String join(final Object[] array, final char separator) {
        if (array == null) {
            return null;
        }// w w w .j a v  a2  s .  c  o  m
        return join(array, separator, 0, array.length);
    }

    public static String join(final Object[] array, final char separator, final int startIndex,
            final int endIndex) {
        if (array == null) {
            return null;
        }
        final int noOfItems = endIndex - startIndex;
        if (noOfItems <= 0) {
            return EMPTY;
        }
        final 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(final Iterable<?> iterable, final char separator) {
        if (iterable == null) {
            return null;
        }
        return join(iterable.iterator(), separator);
    }

    public static String join(final Iterator<?> iterator, final char separator) {

        // handle null, zero and one elements before building a buffer
        if (iterator == null) {
            return null;
        }
        if (!iterator.hasNext()) {
            return EMPTY;
        }
        final Object first = iterator.next();
        if (!iterator.hasNext()) {
            return String.valueOf(first);
        }

        // two or more elements
        final StringBuilder buf = new StringBuilder(256); // Java default is 16,
        // probably too
        // small
        if (first != null) {
            buf.append(first);
        }

        while (iterator.hasNext()) {
            buf.append(separator);
            final Object obj = iterator.next();
            if (obj != null) {
                buf.append(obj);
            }
        }

        return buf.toString();
    }

    public static String join(final int[] array, final char separator) {
        if (array == null) {
            return null;
        }
        return join(array, separator, 0, array.length);
    }

    public static String join(final int[] array, final char separator, final int startIndex, final int endIndex) {
        if (array == null) {
            return null;
        }
        final int noOfItems = endIndex - startIndex;
        if (noOfItems <= 0) {
            return EMPTY;
        }
        final StringBuilder buf = new StringBuilder(noOfItems * 16);
        for (int i = startIndex; i < endIndex; i++) {
            if (i > startIndex) {
                buf.append(separator);
            }
            buf.append(array[i]);
        }
        return buf.toString();
    }
}

Related

  1. join(byte[]... bs)
  2. join(char sep, Object[] array)
  3. join(CharSequence separator, String[] strings)
  4. join(CharSequence[] strings, CharSequence separator)
  5. join(final Object[] array)
  6. join(final Object[] array, final char separator)
  7. join(final String separator, final String[] array)
  8. join(final String[] arrays, final String seperator)
  9. join(final String[] in, final String sep)