Java List from Array asList(int[] ai)

Here you can find the source of asList(int[] ai)

Description

as List

License

Open Source License

Parameter

Parameter Description
ai a parameter

Return

a List containing the elements of the given int array.

Declaration

public static List<Integer> asList(int[] ai) 

Method Source Code

//package com.java2s;
//it under the terms of the GNU Affero General Public License as published by

import java.util.ArrayList;

import java.util.Collection;

import java.util.List;

public class Main {
    /**//from  w w  w.j a  v  a  2 s.c  o m
     * @todo check if this method is still necessary with Java 5.
     * @param ai
     * @return a List<Integer> containing the elements of the given int array.
     * @precondition ai != null
     * @postcondition result != null
     * @postcondition result.size() == ai.length
     */
    public static List<Integer> asList(int[] ai) {
        final List<Integer> result = new ArrayList<Integer>(ai.length);
        for (int i : ai) {
            result.add(i);
        }
        assert result.size() == ai.length;
        return result;
    }

    /**
     * @param at
     * @return a List containing the elements of the given array.
     * @precondition at != null
     * @postcondition result != null
     * @postcondition result.size() == at.length
     */
    public static <T> List<T> asList(T[] at) {
        final List<T> result = new ArrayList<T>(at.length);
        for (T t : at)
            result.add(t);
        assert result.size() == at.length;
        return result;
    }

    /**
     * Returns a new list with {@code t1} as first element followed by
     * all elements of {@code ts}.
     */
    public static <T> List<T> asList(T t1, T... ts) {
        List<T> list = new ArrayList<T>(1 + ts.length);
        list.add(t1);
        for (T t : ts)
            list.add(t);
        return list;
    }

    /**
     * @param coll
     * @postcondition (coll == null) --> (result == 0)
     * @postcondition (coll != null) --> (result == coll.size())
     */
    public static int size(Collection<?> coll) {
        return (coll == null) ? 0 : coll.size();
    }
}

Related

  1. asList(float[] elements)
  2. asList(int... array)
  3. asList(int... data)
  4. asList(int... values)
  5. asList(int[] a)
  6. asList(int[] ar)
  7. asList(int[] arr)
  8. asList(int[] ints)
  9. asList(int[] list)