Java List to Double Array toDoubleArray(final List list)

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

Description

to Double Array

License

Open Source License

Declaration

public static <E> double[] toDoubleArray(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> double[] toDoubleArray(final List<E> list) {
        return toDoubleArray(list, Double.NaN);
    }//from  w w  w.ja v a2s  .c  o  m

    public static <E> double[] toDoubleArray(final List<E> list, Double defaultValue) {
        double[] doubleArray = null;
        if (list != null) {
            doubleArray = new double[list.size()];
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i) != null) {
                    try {
                        doubleArray[i] = Double.parseDouble(list.get(i).toString());
                    } catch (NumberFormatException nfe) {
                        doubleArray[i] = defaultValue;
                    }
                } else {
                    doubleArray[i] = defaultValue;
                }
            }
        }
        return doubleArray;
    }

    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. toDoubleArray(final List numbers)
  2. toDoubleArray(List a)
  3. toDoubleArray(List list)
  4. toDoubleArray(List list)
  5. toDoubleArray(List list)