Java Array to List arrayToCommaList(Object[] array)

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

Description

Format an array of Object as a list with commas, like "apples, oranges, and bananas"); XXX Should have a boolean for the final comma :-)

License

Apache License

Parameter

Parameter Description
array The objects to be stringified

Return

a pretty list

Declaration

public static String arrayToCommaList(Object[] array) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*from   w w w.ja v a 2  s.c  o m*/
     * Format an array of Object as a list with commas,
     * like "apples, oranges, and bananas");
     * XXX Should have a boolean for the final comma :-)
     *
     * @param array The objects to be stringified
     * @return a pretty list
     */
    public static String arrayToCommaList(Object[] array) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < array.length; i++) {
            if (i > 0 && i < array.length - 1) {
                sb.append(',');
            }
            if (i > 0) {
                sb.append(' ');
            }
            if (i == (array.length - 1)) {
                sb.append("and ");
            }
            sb.append(array[i]);
        }
        return sb.toString();
    }
}

Related

  1. arrayToCommaList(String[] array)
  2. arrayToList(final T... items)
  3. arrayToList(int[] intArr, int cnt)
  4. arrayToList(Object[] objs)