Java List from Array arrayAsList(Object value)

Here you can find the source of arrayAsList(Object value)

Description

Transforms any array to a matching list

License

Apache License

Parameter

Parameter Description
value something that might be an array

Return

List representation if passed in value was an array, null otherwise

Declaration

@SuppressWarnings({ "unchecked", "rawtypes" })
public static List<Object> arrayAsList(Object value) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.ArrayList;
import java.util.Arrays;

import java.util.List;

public class Main {
    /**//ww w .j a v a 2 s .  co m
     * Transforms any array to a matching list
     * 
     * @param value
     *            something that might be an array
     * @return List representation if passed in value was an array,
     *         <code>null</code> otherwise
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static List<Object> arrayAsList(Object value) {
        List list = null;
        if (value instanceof int[]) {
            list = new ArrayList();
            int[] array = (int[]) value;
            for (int i : array) {
                list.add(i);
            }
        } else if (value instanceof short[]) {
            list = new ArrayList();
            short[] array = (short[]) value;
            for (short i : array) {
                list.add(i);
            }
        } else if (value instanceof char[]) {
            list = new ArrayList();
            char[] array = (char[]) value;
            for (char i : array) {
                list.add(i);
            }
        } else if (value instanceof byte[]) {
            list = new ArrayList();
            byte[] array = (byte[]) value;
            for (byte i : array) {
                list.add(i);
            }
        } else if (value instanceof long[]) {
            list = new ArrayList();
            long[] array = (long[]) value;
            for (long i : array) {
                list.add(i);
            }
        } else if (value instanceof double[]) {
            list = new ArrayList();
            double[] array = (double[]) value;
            for (double i : array) {
                list.add(i);
            }
        } else if (value instanceof float[]) {
            list = new ArrayList();
            float[] array = (float[]) value;
            for (float i : array) {
                list.add(i);
            }
        } else if (value instanceof boolean[]) {
            list = new ArrayList();
            boolean[] array = (boolean[]) value;
            for (boolean i : array) {
                list.add(i);
            }
        } else if (value.getClass().isArray()) {
            Object[] array = (Object[]) value;
            list = Arrays.asList(array);
        }
        return list;
    }
}

Related

  1. array2ArrayList(final T[] array)
  2. array2List(Object[] obj)
  3. array2list(String[] array)
  4. array2List(T... array)
  5. arrayAsArrayList(E... elements)
  6. arrayAsList(Object[] array)
  7. arrayAsListCopy(T... array)
  8. arrayAsListEmptyIfNull(T[] array)
  9. arrayContainsValue(final String[] list, final String value)