Java List from Array arrayAsList(Object[] array)

Here you can find the source of arrayAsList(Object[] array)

Description

Replacement for Arrays.asList(..) which gacks on null and returns a List in which remove is an unsupported operation.

License

Open Source License

Parameter

Parameter Description
array the Object[] to convert (may be null)

Return

the List corresponding to array (never null)

Declaration

public static List<Object> arrayAsList(Object[] array) 

Method Source Code

//package com.java2s;
/* *******************************************************************
 * Copyright (c) 1999-2001 Xerox Corporation, 
 *               2002 Palo Alto Research Center, Incorporated (PARC).
 * All rights reserved. /* w w  w .  j  a va2 s .co m*/
 * This program and the accompanying materials are made available 
 * under the terms of the Eclipse Public License v1.0 
 * which accompanies this distribution and is available at 
 * http://www.eclipse.org/legal/epl-v10.html 
 *  
 * Contributors: 
 *     Xerox/PARC     initial implementation 
 * ******************************************************************/

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

import java.util.Collections;

import java.util.List;

public class Main {
    /**
     * Replacement for Arrays.asList(..) which gacks on null and returns a List in which remove is an unsupported operation.
     * 
     * @param array the Object[] to convert (may be null)
     * @return the List corresponding to array (never null)
     */
    public static List<Object> arrayAsList(Object[] array) {
        if ((null == array) || (1 > array.length)) {
            return Collections.emptyList();
        }
        ArrayList<Object> list = new ArrayList<Object>();
        list.addAll(Arrays.asList(array));
        return list;
    }
}

Related

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