This method converts an array into an ArrayList. - Android java.util

Android examples for java.util:List Convert

Description

This method converts an array into an ArrayList.

Demo Code


//package com.book2s;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

public class Main {
    public static void main(String[] argv) {
        Object[] array = new String[] { "1", "abc", "level", null,
                "book2s.com", "asdf 123" };
        System.out.println(toArrayList(array));
    }//  ww w  .  j  a  v  a2  s.  c o m

    /**
     * <P>This method converts an array into an ArrayList. (The reverse of a Collection.toArray() method).</P>
     *
     * @param array The array of objects we want within an ArrayList.
     * @return the collection of objects originally contained on the array parameter.
     */
    public static Collection<Object> toArrayList(Object[] array) {
        if (array != null && array.length > 0) {
            ArrayList<Object> result = new ArrayList<Object>(array.length);
            Collections.addAll(result, array);
            return result;
        }
        return null;
    }
}

Related Tutorials