Java List to Double Array toDoubleList(String[] array)

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

Description

Converts a String array to a double array.

License

Open Source License

Parameter

Parameter Description
array Input array

Return

New double array

Declaration

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

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2016 Pablo Pavon-Marino.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * Contributors:/* w w  w .ja  va2 s  .c o  m*/
 *     Pablo Pavon-Marino - Jose-Luis Izquierdo-Zaragoza, up to version 0.3.1
 *     Pablo Pavon-Marino - from version 0.4.0 onwards
 ******************************************************************************/

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Converts a {@code String} array to a {@code double} array.
     *
     * @param array Input array
     * @return New {@code double} array
     * 
     */
    public static List<Double> toDoubleList(String[] array) {
        List<Double> out = new ArrayList<Double>(array.length);
        for (int i = 0; i < array.length; i++) {
            if (array[i] == null)
                throw new RuntimeException("Null value in position " + i);
            out.add(Double.parseDouble(array[i]));
        }
        return out;
    }

    /**
     * Converts a {@code String} array to a {@code double} array.
     *
     * @param array Input array
     * @param valueForNull Value for {@code null} positions
     * @return New {@code double} array
     * 
     */
    public static List<Double> toDoubleList(String[] array, double valueForNull) {
        List<Double> out = new ArrayList<Double>(array.length);
        for (String array1 : array) {
            out.add(array1 == null ? valueForNull : Double.parseDouble(array1));
        }

        return out;
    }
}

Related

  1. toDoubleList(double[] doubleArray)
  2. toDoubleList(Double[] in)
  3. toDoubleList(final E[] array)
  4. toDoubleList(List values)
  5. toDoubleList(List list)