集册 Java实例教程 将数组转换为列表

将数组转换为列表

欢马劈雪     最近更新时间:2020-01-02 10:19:05

391
将数组转换为列表
/** from 时   代    Java - nowjava.com**/

//package com.nowjava;

import java.util.ArrayList;


import java.util.Collections;


import java.util.List;


public class Main {

    @SafeVarargs

    public static <E> List<E> asList(E... elements) {

        if (elements == null || elements.length == 0) {

            return Collections.emptyList();

        }

        int capacity = computeListCapacity(elements.length);

        ArrayList<E> list = new ArrayList<E>(capacity);

        Collections.addAll(list, elements);

        return list;

    }

    /** 
    来 自 
    N o  w  J a v a . c o m - 时  代  Java
    **/

    static int computeListCapacity(
展开阅读全文