Java List to Array toArray(Collection list)

Here you can find the source of toArray(Collection list)

Description

Converts a collection ( List , Set ...) of Double objects to a double array.

License

Open Source License

Parameter

Parameter Description
list Input list

Return

int array

Declaration

public static double[] toArray(Collection<Double> list) 

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:/*from w w w  .j  a v  a  2  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.*;

public class Main {
    /**
     * Converts a collection ({@code List}, {@code Set}...) of {@code Double} objects to a {@code double} array.
     *
     * @param list Input list
     * @return {@code int} array
     */
    public static double[] toArray(Collection<Double> list) {
        return asPrimitiveArray(list.toArray(new Double[list.size()]));
    }

    /**
     * Converts an array of {@code String} with the values, into a {@code double} array. Uses {@code Double.parseDouble} to parse the number, an can raise the exceptions 
     * that this method raises
     *
     * @param list Input list
     * @return output array
     */
    public static double[] toArray(String[] list) {
        double[] res = new double[list.length];
        int counter = 0;
        for (String s : list)
            res[counter++] = Double.parseDouble(s);
        return res;
    }

    /**
     * Converts from a {@code Double} array to a {@code double} array.
     *
     * @param array {@code Double} array
     * @return Equivalent {@code double} array
     */
    public static double[] asPrimitiveArray(Double[] array) {
        double[] primitiveArray = new double[array.length];

        for (int i = 0; i < array.length; i++)
            primitiveArray[i] = array[i];

        return primitiveArray;
    }
}

Related

  1. ListToArray(List> values)
  2. listToArray(List list)
  3. listToArray(List stringList)
  4. listToArray(List list)
  5. toArray(Collection list)
  6. toArray(final List list)
  7. toArray(final List list)
  8. toArray(final List list)
  9. toArray(List list)