Convert List String to String Array - Android java.lang

Android examples for java.lang:array convert

Description

Convert List String to String Array

Demo Code


//package com.java2s;
import android.support.annotation.Nullable;

import java.util.Collection;

import java.util.List;

public class Main {
    /**//from ww w  . j  a va2  s. c om
     * @return a string array of the original list of strings
     */
    @Nullable
    public static String[] toStringArray(List<String> list) {
        if (isEmpty(list)) {
            return null;
        }

        String[] array = new String[list.size()];
        for (int index = 0; index < list.size(); index++) {
            array[index] = list.get(index);
        }

        return array;
    }

    /**
     * Is this list empty. Checks for null as well as size.
     *
     * @return true if empty, false if not
     */
    public static boolean isEmpty(Collection collection) {
        return collection == null || collection.isEmpty();
    }

    /**
     * Is this list empty. Checks for null as well as size.
     *
     * @return true if empty, false if not
     */
    public static boolean isEmpty(Object[] collection) {
        return collection == null || collection.length == 0;
    }
}

Related Tutorials