Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

import java.util.ArrayList;

import java.util.Collection;

import java.util.List;

public class Main {

    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);
        }
    }

    /**
     * Adds the given item to the list at specified <code>index</code>.
     * <p/>
     * if <code>index</code> is greater than list size, it simply appends to the
     * list.
     * 
     * @param list
     *            collection of elements
     * @param index
     *            specific index
     * @param item
     *            object to add.
     */
    public static <E, T extends E> void add(List<E> list, int index, T item) {
        if (index < list.size()) {
            list.add(index, item);
        } else {
            list.add(item);
        }
    }
}