Java Array to String toStringArray(final E[] array)

Here you can find the source of toStringArray(final E[] array)

Description

to String Array

License

Open Source License

Declaration

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

Method Source Code

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

import java.util.List;

public class Main {
    public static <E> String[] toStringArray(final E[] array) {
        String[] stringList = null;
        if (array != null) {
            stringList = new String[array.length];
            for (int i = 0; i < array.length; i++) {
                if (array[i] != null) {
                    stringList[i] = array[i].toString();
                } else {
                    stringList[i] = null;
                }/*w w w .  j ava 2  s  . c o  m*/
            }
        }
        return stringList;
    }

    @Deprecated
    public static <E> String[] toStringArray(final List<E> list) {
        String[] array = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            array[i] = list.get(i).toString();
        }
        return array;
    }

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

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

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

    public static <E> String toString(final double[] list, String separator) {
        StringBuilder sb = new StringBuilder();
        if (list != null && list.length > 0) {
            for (int i = 0; i < list.length - 1; i++) {
                sb.append(list[i]).append(separator);
            }
            sb.append(list[list.length - 1]);
        }
        return sb.toString();
    }

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

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

Related

  1. toString(T[] array)
  2. toString(T[] array)
  3. toString(T[] array, String prefix, String sep, String suffix)
  4. toStringArray(Collection strings)
  5. toStringArray(Dictionary props, String key, String[] defaultArray)
  6. toStringArray(int[] nums)
  7. toStringArray(Object value)
  8. toStringKeyValuePairs(Object[] keys, Object[] values, int size, StringBuilder dstOptional)
  9. toStrings(byte[] bytes)