Here you can find the source of join(T[] array, String separator)
Parameter | Description |
---|---|
T | the generic type |
array | the array |
separator | the separator |
public static <T> String join(T[] array, String separator)
//package com.java2s; /**//from w ww.j a v a2s .c o m * Copyright 2015 IBM Corp. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ import java.util.Arrays; public class Main { /** * Creates a String of all elements of an array, separated by a separator. * * @param <T> the generic type * @param array the array * @param separator the separator * @return the joined String */ public static <T> String join(T[] array, String separator) { return join(Arrays.asList(array), separator); } /** * Creates a String of all elements of an iterable, separated by a separator. * * @param iterable the iterable * @param separator the separator * @return the joined String */ public static String join(Iterable<?> iterable, String separator) { final StringBuilder sb = new StringBuilder(); boolean first = true; for (Object item : iterable) { if (first) { first = false; } else { sb.append(separator); } sb.append(item.toString()); } return sb.toString(); } }