Java List from Array asList(final T... array)

Here you can find the source of asList(final T... array)

Description

Returns the specified array as a List of elements.

License

Open Source License

Parameter

Parameter Description
T the class type of the elements in the array.
array the object array of elements to convert to a List.

Return

a List of elements contained in the specified array.

Declaration

@SafeVarargs
public static <T> List<T> asList(final T... array) 

Method Source Code


//package com.java2s;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;

import java.util.List;

public class Main {
    /**/*  w ww .j av  a  2 s . c  o m*/
     * Returns the specified array as a List of elements.
     * <p/>
     * @param <T> the class type of the elements in the array.
     * @param array the object array of elements to convert to a List.
     * @return a List of elements contained in the specified array.
     * @see java.util.Arrays#asList(Object[])
     */
    @SafeVarargs
    public static <T> List<T> asList(final T... array) {
        List<T> arrayList = new ArrayList<T>(array.length);
        Collections.addAll(arrayList, array);
        return arrayList;
    }

    /**
     * Add all elements of an {@link Enumeration} to a {@link Collection}.
     * 
     * @param collection
     *          to add from enumeration.
     * @param enumeration
     *          to add to collection.
     * @return true if collection is modified, otherwise false.
     * @since 8.1
     * @see Collection#addAll(Collection)
     */
    public static final <T> boolean addAll(final Collection<T> collection, final Enumeration<T> enumeration) {
        if (null == enumeration) {
            return false;
        }

        boolean modified = false;
        while (enumeration.hasMoreElements()) {
            modified |= collection.add(enumeration.nextElement());
        }
        return modified;
    }
}

Related

  1. asList(final Object[] arguments)
  2. asList(final Object[] array)
  3. asList(final Object[] objectArray)
  4. asList(final Set elements)
  5. asList(final T node)
  6. asList(final T... data)
  7. asList(final T... data)
  8. asList(final T[] array)
  9. asList(final T[] input)