Java List Concatenate concat(List list, String delim)

Here you can find the source of concat(List list, String delim)

Description

Used to turn a list into a string with the specified delimiter.

License

Open Source License

Parameter

Parameter Description
list List to Serialize
delim Delimiting Character

Return

Serialized String

Declaration

public static String concat(List<?> list, String delim) 

Method Source Code

//package com.java2s;
/**//from   w  w w . j a va  2 s. c o  m
 * Pennyworth - A new smarthome protocol.
 * Copyright (C) 2012  Dream-Crusher Labs LLC
 *
 * 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

import java.util.List;

public class Main {
    /**
     * Used to turn a list into a string with the specified delimiter.
     * If there are less than 2 items in the list the delimiter will
     * not appear.
     * 
     * @param list List to Serialize
     * @param delim Delimiting Character
     * @return Serialized String
     */
    public static String concat(List<?> list, String delim) {
        String ret = list.get(0) + "";
        for (int i = 1; i < list.size(); i++) {
            ret = ret + delim + list.get(i);
        }
        return ret;
    }

    /**
     * Used to turn a list into a string with the specified delimiter.
     * If there are less than 2 items in the list the delimiter will
     * not appear.
     * 
     * @param list List to Serialize
     * @param delim Delimiting Character
     * @return Serialized String
     */
    public static String concat(Object[] list, String delim) {
        String ret = list[0] + "";
        for (int i = 1; i < list.length; i++) {
            ret = ret + delim + list[i];
        }
        return ret;
    }
}

Related

  1. concat(final Collection... lists)
  2. concat(final List list1, final List list2)
  3. concat(List list)
  4. concat(List l1, List l2)
  5. concat(List list1, List list2)
  6. concat(List lista, List listb)
  7. concat(List items, String quotation, String separation)
  8. concat(List items, String separator)
  9. concat(List strings, String separator)