ArrayList Creation from various parameter - Android java.util

Android examples for java.util:List

Description

ArrayList Creation from various parameter

Demo Code


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

import java.util.Collection;

public class Main {
    public static void main(String[] argv) {
        System.out.println(createArrayList());
    }//from ww w  .jav a  2 s  .c  om

    public static <T> ArrayList<T> createArrayList() {
        return new ArrayList<T>();
    }

    public static <T> ArrayList<T> createArrayList(int initialCapacity) {
        return new ArrayList<T>(initialCapacity);
    }

    public static <T> ArrayList<T> createArrayList(Iterable<? extends T> c) {
        ArrayList<T> list;

        if (c instanceof Collection<?>) {
            list = new ArrayList<T>((Collection<? extends T>) c);
        } else {
            list = new ArrayList<T>();

            iterableToCollection(c, list);

            list.trimToSize();
        }

        return list;
    }

    public static <T, V extends T> ArrayList<T> createArrayList(V... args) {
        if (args == null || args.length == 0) {
            return new ArrayList<T>();
        } else {
            ArrayList<T> list = new ArrayList<T>(args.length);

            for (V v : args) {
                list.add(v);
            }

            return list;
        }
    }

    private static <T> void iterableToCollection(Iterable<? extends T> c,
            Collection<T> list) {
        for (T element : c) {
            list.add(element);
        }
    }
}

Related Tutorials