Java Array Join join(T[] array, String separator)

Here you can find the source of join(T[] array, String separator)

Description

Join an array of items.

License

Open Source License

Parameter

Parameter Description
T a parameter
array a parameter
separator a parameter

Return

string

Declaration

public static <T> String join(T[] array, String separator) 

Method Source Code

//package com.java2s;
// License & terms of use: http://www.unicode.org/copyright.html#License

import java.util.Iterator;

public class Main {
    /**/*from   w  w  w .java 2  s  .  co m*/
     * Join an array of items.
     * @param <T>
     * @param array
     * @param separator
     * @return string
     */
    public static <T> String join(T[] array, String separator) {
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < array.length; ++i) {
            if (i != 0)
                result.append(separator);
            result.append(array[i]);
        }
        return result.toString();
    }

    /**
     * Join a collection of items.
     * @param <T>
     * @param collection 
     * @param <U> 
     * @param array
     * @param separator
     * @return string
     */
    public static <T, U extends Iterable<T>> String join(U collection,
            String separator) {
        StringBuffer result = new StringBuffer();
        boolean first = true;
        for (Iterator it = collection.iterator(); it.hasNext();) {
            if (first)
                first = false;
            else
                result.append(separator);
            result.append(it.next());
        }
        return result.toString();
    }
}

Related

  1. join(String[]... arrays)
  2. join(String[]... values)
  3. join(T[] a, T[] b)
  4. join(T[] arr1, T[] arr2)
  5. join(T[] array, String join)
  6. join(T[] array, String separator)
  7. join(T[] array, String separator)
  8. join(T[] array, String separator)
  9. join(T[] array, String separator)