Java List from Array asList(final int[] a)

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

Description

takes an int array and returns a List of Integer backed by the array

License

Open Source License

Parameter

Parameter Description
a a parameter

Declaration

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

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.AbstractList;
import java.util.ArrayList;

import java.util.List;

public class Main {
    /**//from  w  ww.  j ava  2s. c  o  m
     * takes an int array and returns a List of Integer backed by the array
     * @param a
     * @return
     */
    public static List<Integer> asList(final int[] a) {
        return new AbstractList<Integer>() {
            public Integer get(int i) {
                return a[i];
            }

            // Throws NullPointerException if val == null
            public Integer set(int i, Integer val) {
                Integer oldVal = a[i];
                a[i] = val;
                return oldVal;
            }

            public int size() {
                return a.length;
            }
        };
    }

    /**
     * takes a float array and returns a List of Float backed by the array
     * @param a
     * @return
     */
    public static List<Float> asList(final float[] a) {
        return new AbstractList<Float>() {
            public Float get(int i) {
                return a[i];
            }

            // Throws NullPointerException if val == null
            public Float set(int i, Integer val) {
                Float oldVal = a[i];
                a[i] = val;
                return oldVal;
            }

            public int size() {
                return a.length;
            }
        };
    }

    public static <T> List<T> asList(T... items) {

        ArrayList<T> list = new ArrayList<T>(items.length);
        if (items != null) {
            for (T item : items) {
                list.add(item);
            }
        }
        return list;
    }
}

Related

  1. asList(final char[] array)
  2. asList(final Collection collection)
  3. asList(final Collection c)
  4. asList(final E... array)
  5. asList(final int... values)
  6. asList(final int[] a)
  7. asList(final Iterable iterable)
  8. asList(final Iterable iterable)
  9. asList(final long[] ids)