Java List from Array asList(T[] array)

Here you can find the source of asList(T[] array)

Description

Converts an array to a List This method is similar to Arrays.asList, except that it returns a writeable list

License

Open Source License

Parameter

Parameter Description
array The array to transform into a List

Return

A List containing the same elements as the array

Declaration

public static <T> List<T> asList(T[] array) 

Method Source Code

//package com.java2s;
/*****************************************************************************
 * Copyright (c) 2012 CEA LIST.//from w w w .ja v  a2 s  .c o  m
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *****************************************************************************/

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Converts an array to a List
     * 
     * This method is similar to Arrays.asList, except that it returns
     * a writeable list
     * 
     * @param array
     *        The array to transform into a List
     * @return
     *         A List containing the same elements as the array
     */
    public static <T> List<T> asList(T[] array) {
        if (array == null) {
            return new ArrayList<T>();
        }

        List<T> result = new ArrayList<T>(array.length);
        for (T t : array) {
            result.add(t);
        }
        return result;
    }
}

Related

  1. asList(T... values)
  2. asList(T... values)
  3. asList(T... values)
  4. asList(T[] a)
  5. asList(T[] array)
  6. asList(T[] array)
  7. asList(T[] array)
  8. asList(T[] array)
  9. asListFromObjectArray(Object[] at)