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

    /**
     * Get an integer from the value received
     * @param value Object to convert
     * @return int converted
     */
    public static int getInteger(Object value) {
        if (value instanceof Integer) {
            return ((Integer) value).intValue();
        } else if (value instanceof Double) {
            return ((Double) value).intValue();
        } else if (value instanceof Float) {
            return ((Float) value).intValue();
        } else {
            try {
                return Integer.parseInt(value.toString());
            } catch (NumberFormatException nfe) {
                return 0;
            }
        }
    }
}