Java Double Array Create toDoubleArray(String array)

Here you can find the source of toDoubleArray(String array)

Description

Cast a string to an array of double

String should have the format "[1.2,2.6,3]"

License

Open Source License

Parameter

Parameter Description
array the string to be casted

Return

an array containing the parsed double

Declaration

public static double[] toDoubleArray(String array) 

Method Source Code

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

public class Main {
    /**/*  www . j a v a  2  s .c  om*/
     * Cast a string to an array of double
     * <p>
     * String should have the format <code>"[1.2,2.6,3]"</code>
     * 
     * @param array
     *            the string to be casted
     * @return an array containing the parsed double
     */
    public static double[] toDoubleArray(String array) {
        // Empty array []
        if (array.length() == 2)
            return new double[0];

        String[] cast = array.substring(1, array.length() - 1).split(",");

        double[] result = new double[cast.length];

        for (int i = 0; i < cast.length; i++) {
            result[i] = Double.valueOf(cast[i]);
        }

        return result;
    }
}

Related

  1. toDoubleArray(final long[] array)
  2. toDoubleArray(final Object[] array)
  3. toDoubleArray(int... intArray)
  4. toDoubleArray(int[] ints)
  5. toDoubleArray(Number[] array)
  6. toDoubleArray(String str, String separator)
  7. toDoubleArray(String value)
  8. toDoubleArray(String[] anArray)
  9. toDoubleArray(String[] arr)