Java Array to String array2string(final T[] array, final int top)

Here you can find the source of array2string(final T[] array, final int top)

Description

Converts the array of objects to string, where each array element is separated by comma.

License

Apache License

Parameter

Parameter Description
array the array of objects which need to be converted to string.
top the maximum number of converting array elements.

Return

the string representation of the array.

Declaration

public static final <T> String array2string(final T[] array, final int top) 

Method Source Code

//package com.java2s;
/**//from   w w w  . j  ava  2s  .  co  m
 * Copyright 2014-2015 SHAF-WORK
 * 
 * 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 {
    /**
     * Converts the array of objects to string, where each array element is
     * separated by comma. If the array length is greater than the specified
     * {@code top} value, all array elements which {@code index >= top} will be
     * represented as three dots (...) in the output string.
     * 
     * @param array
     *            the array of objects which need to be converted to string.
     * @param top
     *            the maximum number of converting array elements.
     * @return the string representation of the array.
     */
    public static final <T> String array2string(final T[] array, final int top) {
        StringBuffer buf = new StringBuffer();

        for (int i = 0; i < (array.length > top ? top : array.length); i++) {
            if (i > 0) {
                buf.append(", ");
            }
            buf.append(array[i]);
        }

        if (top < array.length) {
            buf.append(", ...");
        }

        return buf.toString();
    }

    /**
     * Converts the array of objects to string, where each array element is
     * separated by comma.
     * 
     * @param array
     *            the array of objects which need to be converted to string.
     * @return the string representation of the array.
     */
    public static final <T> String array2string(final T[] array) {
        return array2string(array, array.length);
    }
}

Related

  1. array2displaystr(Object[] datas)
  2. array2String(double[] array)
  3. array2String(E[] array)
  4. array2string(int[] _a)
  5. array2String(int[] paramArrayOfInt)
  6. array2String(long[] array)
  7. array2String(long[] array)