Java Array Flatten flatten(Object[] lines, String sep)

Here you can find the source of flatten(Object[] lines, String sep)

Description

Flattens the array into a single, long string.

License

Open Source License

Parameter

Parameter Description
lines the lines to flatten
sep the separator

Return

the generated string

Declaration

public static String flatten(Object[] lines, String sep) 

Method Source Code

//package com.java2s;
/*// w  w  w  .j a  v a 2s  .  com
 *   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/>.
 */

import java.util.List;

public class Main {
    /**
     * Flattens the list into a single, long string. The separator string gets
     * added between the objects, but not after the last one.
     *
     * @param lines   the lines to flatten
     * @param sep      the separator
     * @return      the generated string
     */
    public static String flatten(List lines, String sep) {
        return flatten(lines.toArray(new Object[lines.size()]), sep);
    }

    /**
     * Flattens the array into a single, long string. The separator string gets
     * added between the objects, but not after the last one. Uses the "toString()"
     * method of the objects to turn them into a string.
     *
     * @param lines   the lines to flatten
     * @param sep      the separator
     * @return      the generated string
     */
    public static String flatten(Object[] lines, String sep) {
        StringBuilder result;
        int i;

        result = new StringBuilder();

        for (i = 0; i < lines.length; i++) {
            if (i > 0)
                result.append(sep);
            result.append(lines[i].toString());
        }

        return result.toString();
    }
}

Related

  1. flatten(E[][] a)
  2. flatten(final Object[] array)
  3. flatten(float[][] mat)
  4. flatten(Object[] array)
  5. flatten(Object[] array)
  6. flatten(String s[])
  7. flatten(String[] strings, String separator)
  8. flattenArguments(String[] arguments)
  9. flattenArray(Object... objects)