1-D Integer array to double array. - Java java.lang

Java examples for java.lang:int Array

Description

1-D Integer array to double 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 {
        int[] array = new int[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(java.util.Arrays.toString(toDouble(array)));
    }//from  w  ww  . ja va 2s.c  o  m

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

    /**
     * 2-D Integer array to double array.
     * @param array Integer array.
     * @return Double array.
     */
    public static double[][] toDouble(int[][] array) {
        double[][] n = new double[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] = (double) array[i][j];
            }
        }
        return n;
    }

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

    /**
     * 2-D Float array to double array.
     * @param array Float array.
     * @return Double array.
     */
    public static double[][] toDouble(float[][] array) {
        double[][] n = new double[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] = (double) array[i][j];
            }
        }
        return n;
    }
}

Related Tutorials