Java ArrayList Print writeArrayList(ArrayList objects, String separator, String final_conjunction, String prefix, String suffix)

Here you can find the source of writeArrayList(ArrayList objects, String separator, String final_conjunction, String prefix, String suffix)

Description

write Array List

License

Open Source License

Declaration

public static String writeArrayList(ArrayList<?> objects, String separator, String final_conjunction,
            String prefix, String suffix) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;

public class Main {
    public static String writeArrayList(ArrayList<?> objects, String separator, String final_conjunction,
            String prefix, String suffix) {
        return writeArray(objects.toArray(), separator, final_conjunction, prefix, suffix);
    }/*w w  w. j av a 2 s. c o m*/

    public static String writeArrayList(ArrayList<?> objects, String separator, String final_conjunction,
            String prefix) {
        return writeArrayList(objects, separator, final_conjunction, prefix, null);
    }

    public static String writeArrayList(ArrayList<?> objects, String separator, String final_conjunction) {
        return writeArrayList(objects, separator, final_conjunction, null, null);
    }

    public static String writeArrayList(ArrayList<?> objects, String separator) {
        return writeArrayList(objects, separator, null, null, null);
    }

    public static String writeArrayList(ArrayList<?> objects) {
        return writeArrayList(objects, null, null, null, null);
    }

    /** This method returns a grammatically correct list that contains all of the items given in a String array.
     * 
     * @param objects
     *            is the String array which will be written into a list.
     * @param separator
     *            is the String that will be used to separate terms in the list; by default, the separator is ", "
     * @param final_conjunction
     *            is the String that will be added between the second to last and last terms in a list of two or more items; by default, the final conjunction is "and"
     * @param prefix
     *            is the String that will be added to the beginning of each item in the list; by default, the prefix is an empty String ("")
     * @param suffix
     *            is the String that will be added to the end of each item in the list; by default, the suffix is an empty String ("")
     * @return a grammatically correct list of the objects in <b><tt>objects</b></tt>. */
    public static String writeArray(Object[] objects, String separator, String final_conjunction, String prefix,
            String suffix) {
        // establish default for all the arguments
        if (separator == null)
            separator = ", ";
        if (prefix == null)
            prefix = "";
        if (suffix == null)
            suffix = "";
        if (final_conjunction == null)
            final_conjunction = "and";
        else
            final_conjunction = final_conjunction.trim();

        if (objects.length == 0)
            return "";
        else if (objects.length == 1)
            return prefix + objects[0] + suffix;
        else if (objects.length == 2)
            return objects[0] + " " + final_conjunction + " " + objects[1];
        else {
            String list = "";
            for (int i = 0; i < objects.length; i++) {
                list += prefix + objects[i] + suffix;
                if (i < objects.length - 1) {
                    list += separator;
                    if (i == objects.length - 2 && !final_conjunction.equals(""))
                        list += final_conjunction + " ";
                }
            }
            return list;
        }
    }

    public static String writeArray(Object[] objects, String separator, String final_conjunction, String prefix) {
        return writeArray(objects, separator, final_conjunction, prefix, null);
    }

    public static String writeArray(Object[] objects, String separator, String final_conjunction) {
        return writeArray(objects, separator, final_conjunction, null, null);
    }

    public static String writeArray(Object[] objects, String separator) {
        return writeArray(objects, separator, null, null, null);
    }

    public static String writeArray(Object[] objects) {
        return writeArray(objects, null, null, null, null);
    }
}

Related

  1. printEdgeCycleMembership(final int[][] cyclesData, final ArrayList[] nodeCycleMembership)
  2. printListFlat(ArrayList list)
  3. printMatrix(ArrayList> grid)
  4. printReport(ArrayList words, ArrayList searched)
  5. printStringArrayList(ArrayList list)
  6. writeArrayListToScreen(ArrayList readArrrayList)
  7. writeArrayListToScreen(ArrayList readArrrayList)
  8. writeToTable(String serialColumnValue, ArrayList columnValues)