Java Array to String arrayToString(Object[] array, String delim, int maxValues, boolean useBrackets)

Here you can find the source of arrayToString(Object[] array, String delim, int maxValues, boolean useBrackets)

Description

array To String

License

Apache License

Parameter

Parameter Description
delim what delimiter to use in between array elements.
maxValues how many array elements to include. When less than 0, all values are included. If <code>maxValues</code> is greater than the number of elements in <code>array</code>, then all elements are included. If any elements are not included, <code>...</code> will be inserted after the last element.
useBrackets add [] to outside of string iff true

Declaration

public static String arrayToString(Object[] array, String delim,
        int maxValues, boolean useBrackets) 

Method Source Code

//package com.java2s;
/*/*from  w  w w. j a v  a2s  .c  o  m*/
 * Copyright (C) 2013 salesforce.com, inc.
 *
 * 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.
 */

public class Main {
    /**
     * @param delim what delimiter to use in between array elements.
     * @param maxValues how many array elements to include. When less than 0, all values are included. If
     *            <code>maxValues</code> is greater than the number of elements in <code>array</code>, then all elements
     *            are included. If any elements are not included, <code>...</code> will be inserted after the last
     *            element.
     * @param useBrackets add [] to outside of string iff true
     * @see #collectionToString(Iterable, String, String)
     */
    public static String arrayToString(Object[] array, String delim,
            int maxValues, boolean useBrackets) {
        return arrayToString(array, delim, maxValues, useBrackets, true);
    }

    /**
     * @param delim what delimiter to use in between array elements.
     * @param maxValues how many array elements to include. When less than 0, all values are included. If
     *            <code>maxValues</code> is greater than the number of elements in <code>array</code>, then all elements
     *            are included. If any elements are not included and appendEllipsis is set, <code>...</code> will be
     *            inserted after the last element.
     * @param useBrackets add [] to outside of string iff true
     * @param appendEllipsis if set and, and any elements are not included, <code>...</code> will be inserted after the
     *            last element.
     * @see #collectionToString(Iterable, String, String)
     */
    public static String arrayToString(Object[] array, String delim,
            int maxValues, boolean useBrackets, boolean appendEllipsis) {
        if (delim == null) {
            throw new IllegalArgumentException();
        }
        if (array == null) {
            return null;
        }
        if (array.length == 0) {
            return useBrackets ? "[]" : "";
        }

        int max = maxValues < 0 ? array.length : Math.min(array.length,
                maxValues);
        StringBuilder temp = new StringBuilder(2 + (max * 16));
        if (useBrackets) {
            temp.append('[');
        }
        for (int i = 0; i < max; i++) {
            if (i > 0) {
                temp.append(delim);
            }
            temp.append(array[i]);
        }
        if (max < array.length && appendEllipsis) {
            temp.append("...");
        }

        if (useBrackets) {
            temp.append(']');
        }
        return temp.toString();
    }
}

Related

  1. arrayToString(Object[] array)
  2. arrayToString(Object[] array)
  3. arrayToString(Object[] array)
  4. arrayToString(Object[] array)
  5. ArrayToString(Object[] array, String conv)
  6. arrayToString(Object[] array, String prefix, String suffix, String separator)
  7. arrayToString(Object[] array, String sep)
  8. arrayToString(Object[] array, String separator)
  9. arrayToString(Object[] array, String separator)