Here you can find the source of getArrayPreview(final Object[] array, final int previewSize)
Parameter | Description |
---|---|
array | the array. |
previewSize | the number of elements to enumerate. |
public static String getArrayPreview(final Object[] array, final int previewSize)
//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(); } }