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

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

Description

join

License

Open Source License

Declaration

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

Method Source Code

//package com.java2s;
/*//from   w ww . j  a v  a2  s.co  m
 * Copyright (c) 2012-2016 The ANTLR Project. All rights reserved.
 * Use of this file is governed by the BSD 3-clause license that
 * can be found in the LICENSE.txt file in the project root.
 */

import java.util.Iterator;

public class Main {
    public static <T> String join(Iterator<T> iter, String separator) {
        StringBuilder buf = new StringBuilder();
        while (iter.hasNext()) {
            buf.append(iter.next());
            if (iter.hasNext()) {
                buf.append(separator);
            }
        }
        return buf.toString();
    }

    public static <T> String join(T[] array, String separator) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            builder.append(array[i]);
            if (i < array.length - 1) {
                builder.append(separator);
            }
        }

        return builder.toString();
    }
}

Related

  1. join(T[] array, String join)
  2. join(T[] array, String separator)
  3. join(T[] array, String separator)
  4. join(T[] array, String separator)
  5. join(T[] array, String separator)
  6. join(T[] array1, T[] array2)
  7. join(T[] first, T[] second)
  8. join(T[] objs, String splitString)
  9. joinArrays(T[] a, T[] b)