Java ArrayList Join join(ArrayList list, String delimeter)

Here you can find the source of join(ArrayList list, String delimeter)

Description

Folds the list into a string, putting the delimeter in between each element of the collection.

License

Open Source License

Parameter

Parameter Description
list the list to fold
delimeter string to put in between each element of the list

Return

the joined list string

Declaration

public static String join(ArrayList<?> list, String delimeter) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;

import java.util.Collection;

public class Main {
    /**/*  w  ww  . j av a 2  s .c  o m*/
     * Folds the collection into a string, putting the delimeter in between
     * each element of the collection. eg join([1,2,3], " ") = "1 2 3"
     *
     * @param collection the collection to fold
     * @param delimeter  string to put in between each element of the collection
     * @return the joined collection string
     * @see StringUtil.join(ArrayList<?>, String)
     */
    public static String join(Collection<?> collection, String delimeter) {
        return join(new ArrayList(collection), delimeter);
    }

    /**
     * Folds the list into a string, putting the delimeter in between
     * each element of the collection. eg join([1,2,3], " ") = "1 2 3"
     *
     * @param list      the list to fold
     * @param delimeter string to put in between each element of the list
     * @return the joined list string
     */
    public static String join(ArrayList<?> list, String delimeter) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size() - 1; i++) {
            sb.append(list.get(i).toString()).append(delimeter);
        }
        return sb.append(list.get(list.size() - 1).toString()).toString();
    }

    /**
     * Folds the array into a string, putting the delimeter in between
     * each element of the collection. eg join([1,2,3], " ") = "1 2 3"
     *
     * @param array     to array to fold
     * @param delimeter string to put in between each element of the list
     * @return the joined array string
     */
    public static String join(Object[] array, String delimeter) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < array.length - 1; i++) {
            sb.append(array[i].toString()).append(delimeter);
        }
        return sb.append(array[array.length - 1].toString()).toString();
    }
}

Related

  1. join(ArrayList arr, String separator)
  2. Join(ArrayList coll, String delimiter)
  3. join(ArrayList list)
  4. join(ArrayList list, String delimiter)