Java List to Double Array toDoubleList(final E[] array)

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

Description

to Double List

License

Open Source License

Declaration

public static <E> List<Double> toDoubleList(final E[] array) 

Method Source Code

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

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static <E> List<Double> toDoubleList(final E[] array) {
        List<Double> doubleList = null;
        if (array != null) {
            doubleList = new ArrayList<Double>(array.length);
            for (E e : array) {
                if (e != null) {
                    try {
                        doubleList.add(Double.parseDouble(e.toString()));
                    } catch (NumberFormatException nfe) {
                        doubleList.add(null);
                    }/*w w w  . j a  v a  2  s.  c  om*/
                } else {
                    doubleList.add(null);
                }
            }
        }
        return doubleList;
    }

    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. toDoubleArray(List values)
  2. toDoubleList(double[] array)
  3. toDoubleList(double[] array)
  4. toDoubleList(double[] doubleArray)
  5. toDoubleList(Double[] in)
  6. toDoubleList(List values)
  7. toDoubleList(List list)
  8. toDoubleList(String[] array)