Java List from Array asList(int[] list)

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

Description

Converts an array of primitives to a list of Integers.

License

Open Source License

Declaration

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

Method Source Code


//package com.java2s;
import java.util.ArrayList;

import java.util.List;

public class Main {
    /**//from   w  w  w. j  a  v a2s.c o  m
     * The size of a list to create if we have to create one entirely
     * from scratch rather than just expand it.
     */
    protected static final int DEFAULT_LIST_SIZE = 4;

    /**
     * Converts an array of primitives to a list of Integers.
     */
    public static List<Integer> asList(int[] list) {
        if (list == null) {
            return null;
        }
        List<Integer> ilist = new ArrayList<Integer>(list.length);
        for (int ii = 0; ii < list.length; ii++) {
            ilist.add(list[ii]);
        }
        return ilist;
    }

    /**
     * Adds the specified value to the first empty slot in the specified
     * list. Begins searching for empty slots at zeroth index.
     *
     * @param list the list to which to add the value. Can be null.
     * @param value the value to add.
     *
     * @return a reference to the list with value added (might not be the
     * list you passed in due to expansion, or allocation).
     */
    public static int[] add(int[] list, int value) {
        return add(list, 0, value);
    }

    /**
     * Adds the specified value to the next empty slot in the specified
     * list. Begins searching for empty slots at the specified index. This
     * can be used to quickly add values to a list that preserves
     * consecutivity by calling it with the size of the list as the first
     * index to check.
     *
     * @param list the list to which to add the value. Can be null.
     * @param startIdx the index at which to start looking for a spot.
     * @param value the value to add.
     *
     * @return a reference to the list with the value added (might not be
     * the list you passed in due to expansion, or allocation).
     */
    public static int[] add(int[] list, int startIdx, int value) {
        // make sure we've got a list to work with
        if (list == null) {
            list = new int[DEFAULT_LIST_SIZE];
        }

        // search for a spot to insert yon value; assuming we'll insert
        // it at the end of the list if we don't find one
        int llength = list.length;
        int index = llength;
        for (int i = startIdx; i < llength; i++) {
            if (list[i] == 0) {
                index = i;
                break;
            }
        }

        // expand the list if necessary
        if (index >= list.length) {
            list = accomodate(list, index);
        }

        // stick the value on in
        list[index] = value;

        return list;
    }

    /**
     * Creates a new list that will accomodate the specified index and
     * copies the contents of the old list to the first.
     */
    protected static int[] accomodate(int[] list, int index) {
        int size = list.length;
        // expand size by powers of two until we're big enough
        while (size <= index) {
            size = Math.max(size * 2, DEFAULT_LIST_SIZE);
        }

        // create a new list and copy the contents
        int[] newlist = new int[size];
        System.arraycopy(list, 0, newlist, 0, list.length);
        return newlist;
    }
}

Related

  1. asList(int[] a)
  2. asList(int[] ai)
  3. asList(int[] ar)
  4. asList(int[] arr)
  5. asList(int[] ints)
  6. asList(int[] values)
  7. asList(Iterable iterable)
  8. asList(Iterable iterable)
  9. asList(Iterable iterable)