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

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

Description

join

License

Apache License

Declaration

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

Method Source Code

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

import java.util.Iterator;

public class Main {
    public static String join(Object[] array, char separator) {
        if (array == null) {
            return null;
        }//from www  .ja v a 2s  . c om
        int arraySize = array.length;
        int bufSize = arraySize == 0 ? 0 : ((array[0] == null ? 16 : array[0].toString().length()) + 1) * arraySize;
        StringBuffer buf = new StringBuffer(bufSize);
        for (int i = 0; i < arraySize; i++) {
            if (i > 0) {
                buf.append(separator);
            }
            if (array[i] != null) {
                buf.append(array[i]);
            }
        }
        return buf.toString();
    }

    public static String join(Object[] array) {
        return join(array, null);
    }

    public static String join(Object[] array, String separator) {
        if (array == null) {
            return null;
        }
        if (separator == null) {
            separator = "";
        }
        int arraySize = array.length;

        int bufSize = arraySize == 0 ? 0
                : arraySize * ((array[0] == null ? 16 : array[0].toString().length()) + separator.length());

        StringBuffer buf = new StringBuffer(bufSize);
        for (int i = 0; i < arraySize; i++) {
            if (i > 0) {
                buf.append(separator);
            }
            if (array[i] != null) {
                buf.append(array[i]);
            }
        }
        return buf.toString();
    }

    public static String join(Iterator<?> iterator, char separator) {
        if (iterator == null) {
            return null;
        }
        StringBuffer buf = new StringBuffer(256);
        while (iterator.hasNext()) {
            Object obj = iterator.next();
            if (obj != null) {
                buf.append(obj);
            }
            if (iterator.hasNext()) {
                buf.append(separator);
            }
        }
        return buf.toString();
    }

    public static String join(Iterator<?> iterator, String separator) {
        if (iterator == null) {
            return null;
        }
        StringBuffer buf = new StringBuffer(256);
        while (iterator.hasNext()) {
            Object obj = iterator.next();
            if (obj != null) {
                buf.append(obj);
            }
            if ((separator != null) && (iterator.hasNext())) {
                buf.append(separator);
            }
        }
        return buf.toString();
    }
}

Related

  1. join(Object[] array)
  2. join(Object[] array)
  3. join(Object[] array)
  4. join(Object[] array)
  5. join(Object[] array)
  6. join(Object[] array, char separator)
  7. join(Object[] array, char separator)
  8. join(Object[] array, char separator)
  9. join(Object[] array, char separator, int startIndex, int endIndex)