An easy way to construct an array list from Elements. - Java java.util

Java examples for java.util:List Element

Description

An easy way to construct an array list from Elements.

Demo Code


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

public class Main {
    /**/*from  w  w w.  j  a v a  2s .co m*/
     * An easy way to construct an array list. just do
     * 
     * <pre>
     * ArrayList&lt;String&gt; al = CollectionHelper.ArrayList(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;);
     * </pre>
     * 
     * Rather than
     * 
     * <pre>
     * ArrayList&lt;String&gt; al = new ArrayList&lt;String&gt;()
     * al.add(&quot;a&quot;);
     * al.add(&quot;b&quot;);
     * al.add(&quot;c&quot;);
     * </pre>
     * 
     * @param elements
     *            the elements to create an ArrayList of
     * @return a freshly created <tt>ArrayList</tt> containing elements given as arguments. If elements is null, null
     *         is returned.
     * @since 0.1
     */
    public static <T> ArrayList<T> arrayList(final T... elements) {
        if (elements == null)
            return null;
        final ArrayList<T> result = new ArrayList<T>(elements.length);
        for (final T elem : elements) {
            result.add(elem);
        }
        return result;
    }
}

Related Tutorials