Java Array Flatten flatten(final Object[] array)

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

Description

Flattens the elements of the provided array into a single string, separating elements by a space character.

License

Open Source License

Parameter

Parameter Description
array the array of values to join

Return

the flattened string

Declaration

public static String flatten(final Object[] array) 

Method Source Code

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

import java.util.Iterator;

public class Main {
    /**/*www  .  j  a  va  2  s. co m*/
     * Default separator used to {@linkplain #flatten(Object[]) flatten} array
     * if no other separator is specified: {@value} (a single space).
     */
    public static final String SEPARATOR = " ";

    /**
     * Flattens the objects returned by an iterator into a single string,
     * separating elements by a space character.
     *
     * @param iterator the iterator over the elements to join
     * @return the flattened string
     */
    public static String flatten(final Iterator iterator) {
        return flatten(iterator, SEPARATOR);
    }

    /**
     * Flattens the objects returned by an iterator into a single string,
     * separating elements by the provided separator.
     *
     * @param iterator the iterator over the elements to join
     * @param separator the separator string to use
     * @return the flattened string
     */
    public static String flatten(final Iterator iterator,
            final String separator) {
        final StringBuilder result = new StringBuilder();

        while (iterator.hasNext()) {
            result.append(iterator.next().toString());
            if (iterator.hasNext()) {
                result.append(separator);
            }
        }
        return result.toString();
    }

    /**
     * Flattens the elements of the provided array into a single string,
     * separating elements by a space character.
     *
     * @param array the array of values to join
     * @return the flattened string
     */
    public static String flatten(final Object[] array) {
        return flatten(array, SEPARATOR);
    }

    /**
     * Flattens the elements of the provided array into a single string,
     * separating elements by the provided separator.
     *
     * @param array the array of values to join
     * @param separator the separator string to use
     * @return the flattened string
     */
    public static String flatten(final Object[] array,
            final String separator) {
        final StringBuilder result = new StringBuilder();

        for (int i = 0; i < array.length; i++) {
            if (i > 0) {
                result.append(separator);
            }
            result.append(array[i]);
        }
        return result.toString();
    }
}

Related

  1. flatten(byte[][] first)
  2. flatten(E[][] a)
  3. flatten(float[][] mat)
  4. flatten(Object[] array)
  5. flatten(Object[] array)
  6. flatten(Object[] lines, String sep)