Java List to Array toStringArray(final List list)

Here you can find the source of toStringArray(final List list)

Description

to String Array

License

Open Source License

Declaration

public static <E> String[] toStringArray(final List<E> list) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.*;

public class Main {
    public static <E> String[] toStringArray(final List<E> list) {
        return toStringArray(list, null);
    }//from w  ww .j a  v a  2s.  c  o m

    public static <E> String[] toStringArray(final List<E> list, String defaultValue) {
        String[] array = null;
        if (list != null) {
            array = new String[list.size()];
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i) != null) {
                    array[i] = list.get(i).toString();
                } else {
                    array[i] = defaultValue;
                }
            }
        }
        return array;
    }

    public static <E> String toString(final List<E> list) {
        return toString(list, "\t");
    }

    public static <E> String toString(final List<E> list, String separator) {
        StringBuilder sb = new StringBuilder();
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size() - 1; i++) {
                if (list.get(i) != null) {
                    sb.append(list.get(i).toString()).append(separator);
                } else {
                    sb.append("null").append(separator);
                }
            }
            if (list.get(list.size() - 1) != null) {
                sb.append(list.get(list.size() - 1).toString());
            } else {
                sb.append("null");
            }
        }
        return sb.toString();
    }
}

Related

  1. toObjectArray(List> lists)
  2. toObjectArray(List list)
  3. toObjectArray(List list)
  4. toObjectArrayNative(List L)
  5. toStringArray(final List stringList)
  6. toStringArray(List l)
  7. toStringArray(List list)
  8. toStringArray(List list)
  9. toStringArray(List objList)

    HOME | Copyright © www.java2s.com 2016