Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.Array;

import java.util.List;

public class Main {
    /**
     * Get a double array from a list given
     * @param list List with Objects that can be translated to double
     * @return double[]
     */
    public static double[] getDoubleArray(List list) {
        double[] array = new double[list.size()];
        for (int i = 0; i < array.length; i++) {
            Array.set(array, i, getDouble(list.get(i)));
        }
        return array;
    }

    /**
     * Get a double from the value received
     * @param value Object to convert
     * @return int converted
     */
    public static double getDouble(Object value) {
        if (value instanceof Integer) {
            return ((Integer) value).doubleValue();
        } else if (value instanceof Double) {
            return ((Double) value).doubleValue();
        } else if (value instanceof Float) {
            return ((Float) value).doubleValue();
        } else {
            try {
                return Double.parseDouble(value.toString());
            } catch (NumberFormatException nfe) {
                return 0;
            }
        }
    }
}