Java Array Join join(final Object[] array)

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

Description

join

License

Open Source License

Declaration

public static String join(final Object[] array) 

Method Source Code


//package com.java2s;
/*/*from  www.ja va 2s.c o  m*/
 * #%L
 * UCoin Java :: Core Shared
 * %%
 * Copyright (C) 2014 - 2016 EIS
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */

import java.util.Collection;
import java.util.Iterator;

public class Main {
    public static String join(final Object[] array) {
        return join(array, ", ");
    }

    public static String join(final Object[] array, final String separator) {
        if (array == null || array.length == 0) {
            return null;
        }
        StringBuilder sb = new StringBuilder(array.length * 7);
        sb.append(array[0]);
        for (int i = 1; i < array.length; i++) {
            sb.append(separator);
            sb.append(array[i]);
        }
        return sb.toString();
    }

    public static String join(final Collection<?> collection) {
        return join(collection, ", ");
    }

    public static String join(final Collection<?> collection, final String separator) {
        if (collection == null || collection.size() == 0) {
            return null;
        }
        StringBuilder sb = new StringBuilder(collection.size() * 7);
        Iterator<?> iterator = collection.iterator();
        sb.append(iterator.next());
        while (iterator.hasNext()) {
            sb.append(separator);
            sb.append(iterator.next());
        }
        return sb.toString();
    }

    public static int size(final Collection<?> collection) {
        return collection == null ? 0 : collection.size();
    }
}

Related

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