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 float array from a list given
     * @param list List with Objects that can be translated to float
     * @return float[]
     */
    public static float[] getFloatArray(List list) {
        float[] array = new float[list.size()];
        for (int i = 0; i < array.length; i++) {
            Array.set(array, i, getFloat(list.get(i)));
        }
        return array;
    }

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