Java Array Element Get getArrayPreview(final Object[] array, final int previewSize)

Here you can find the source of getArrayPreview(final Object[] array, final int previewSize)

Description

Generates a string that enumerates just the first elements of an array (a preview).

License

Open Source License

Parameter

Parameter Description
array the array.
previewSize the number of elements to enumerate.

Return

a string with the first previewSize elements of the collection.

Declaration

public static String getArrayPreview(final Object[] array, final int previewSize) 

Method Source Code


//package com.java2s;
import java.util.*;

public class Main {
    /**/*from   w  w  w.j av  a  2s .co m*/
     * Separator string for lists.
     */
    public static final String LIST_SEPARATOR = ",";

    /**
     * Generates a string that enumerates just the first elements of an array (a preview).
     *
     * @param array  the array.
     * @param previewSize the number of elements to enumerate.
     * @return a string with the first <i>previewSize</i> elements of the collection.
     */
    public static String getArrayPreview(final Object[] array, final int previewSize) {
        return getCollectionPreview(Arrays.asList(array), previewSize);
    }

    /**
     * Generates a string that enumerates just the first elements of a collection (a preview).
     *
     * @param collection  the collection.
     * @param previewSize the number of elements to enumerate.
     * @return a string with the first <i>previewSize</i> elements of the collection.
     */
    public static String getCollectionPreview(final Collection<?> collection, int previewSize) {
        final Iterator<?> it = collection.iterator();
        final StringBuilder buffer = new StringBuilder();
        if (it.hasNext()) {
            buffer.append('[').append(it.next().toString());
            for (; previewSize > 0 && it.hasNext(); --previewSize)
                buffer.append(LIST_SEPARATOR).append(' ').append(it.next().toString());
            if (it.hasNext())
                buffer.append(" ... ");
            buffer.append(']');
        }
        return buffer.toString();
    }
}

Related

  1. getArgPairsSeparatedByChar(String[] args, String separator)
  2. getArgs(final String[] args, final String[] defaultValue, final String... flags)
  3. getArgs(String[] args, String... singleArgs)
  4. getArgsOption(String[] args, String option)
  5. getArrayNoNull(Object[] array)
  6. getArrays(byte[] inputArray, int arraySize, boolean zeroPad)
  7. getArraySubset(T[] array, int start, int end)
  8. getFrequentElement(int[] bcp)
  9. getFrequentElement(int[] bcp)