1-D Double array to integer array. - Java java.lang

Java examples for java.lang:int Array

Description

1-D Double array to integer array.

Demo Code

//    This library is free software; you can redistribute it and/or
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        double[] array = new double[] { 34.45, 35.45, 36.67, 37.78,
                37.0000, 37.1234, 67.2344, 68.34534, 69.87700 };
        System.out.println(java.util.Arrays.toString(toInt(array)));
    }/*www  .  ja  va2s  .c  o m*/

    /**
     * 1-D Double array to integer array.
     * @param array Double array.
     * @return Integer array.
     */
    public static int[] toInt(double[] array) {
        int[] n = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            n[i] = (int) array[i];
        }
        return n;
    }

    /**
     * 2-D Double array to integer array.
     * @param array Double array.
     * @return Integer array.
     */
    public static int[][] toInt(double[][] array) {
        int[][] n = new int[array.length][array[0].length];
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[0].length; j++) {
                n[i][j] = (int) array[i][j];
            }
        }
        return n;
    }

    /**
     * 1-D Float array to integer array.
     * @param array Float array.
     * @return Integer array.
     */
    public static int[] toInt(float[] array) {
        int[] n = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            n[i] = (int) array[i];
        }
        return n;
    }

    /**
     * 2-D Float array to integer array.
     * @param array Float array.
     * @return Integer array.
     */
    public static int[][] toInt(float[][] array) {
        int[][] n = new int[array.length][array[0].length];
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[0].length; j++) {
                n[i][j] = (int) array[i][j];
            }
        }
        return n;
    }
}

Related Tutorials