Java ArrayList Create newArrayList(T anObj)

Here you can find the source of newArrayList(T anObj)

Description

Creates a new array list with given object.

License

Open Source License

Declaration

public static <T> List<T> newArrayList(T anObj) 

Method Source Code


//package com.java2s;

import java.util.*;

public class Main {
    /**//  w  ww .  ja  v  a  2s . c  om
     * Creates a new array list with given object.
     */
    public static <T> List<T> newArrayList(T anObj) {
        return newArrayList(10, anObj);
    }

    /**
     * Creates a new array list with given object and capacity.
     */
    public static <T> List<T> newArrayList(int aCapacity, T anObj) {
        List list = new ArrayList(aCapacity);
        if (anObj != null)
            list.add(anObj);
        return list;
    }

    /**
     * Creates a new array list with given objects.
     */
    public static <T> List<T> newArrayList(T... theObjects) {
        return newArrayList(theObjects.length, theObjects);
    }

    /**
     * Creates a new array list with given objects and capacity.
     */
    public static <T> List<T> newArrayList(int aCapacity, T... theObjects) {
        List list = new ArrayList(Math.min(theObjects.length, aCapacity));
        for (T item : theObjects)
            list.add(item);
        return list;
    }

    /**
     * Adds an object to the given list and returns list (creates list if missing).
     */
    public static <T> List<T> add(List<T> aList, T anObj) {
        // If list is null, create list
        if (aList == null)
            aList = new Vector();

        // Add object
        aList.add(anObj);

        // Return list
        return aList;
    }
}

Related

  1. newArrayList(int initialCapacity)
  2. newArrayList(int initialCapacity)
  3. newArrayList(int size)
  4. newArrayList(Iterable elements)
  5. newArrayList(Iterable iterable)
  6. newArrayList(T... elements)
  7. newArrayList(T... items)
  8. newArrayList(T... objectList)
  9. newArrayList(T... objs)