Convert value array to Double array - Java Collection Framework

Java examples for Collection Framework:Array Convert

Description

Convert value array to Double array

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        float[] v = new float[] { 34.45f, 35.45f, 36.67f, 37.78f, 37.0000f,
                37.1234f, 67.2344f, 68.34534f, 69.87700f };
        System.out.println(java.util.Arrays.toString(toDouble(v)));
    }//from   w ww. j  a va  2s .c  o  m

    public static double[] toDouble(float[] v) {
        final double[] ans = new double[v.length];
        for (int k = 0; k < v.length; k++)
            ans[k] = (double) v[k];
        return (ans);
    }

    public static double[] toDouble(long[] v) {
        final double[] ans = new double[v.length];
        for (int k = 0; k < v.length; k++)
            ans[k] = (double) v[k];
        return (ans);
    }

    public static double[] toDouble(int[] v) {
        final double[] ans = new double[v.length];
        for (int k = 0; k < v.length; k++)
            ans[k] = (double) v[k];
        return (ans);
    }

    public static double[][] toDouble(float[][] v) {
        final double[][] ans = new double[v.length][];
        for (int k = 0; k < v.length; k++)
            ans[k] = toDouble(v[k]);
        return (ans);
    }

    public static double[][] toDouble(long[][] v) {
        final double[][] ans = new double[v.length][];
        for (int k = 0; k < v.length; k++)
            ans[k] = toDouble(v[k]);
        return (ans);
    }

    public static double[][] toDouble(int[][] v) {
        final double[][] ans = new double[v.length][];
        for (int k = 0; k < v.length; k++)
            ans[k] = toDouble(v[k]);
        return (ans);
    }

    /**
     * Returns a comma delimited string representing the value of the array.
     */
    public static String toString(double[] array) {

        StringBuffer buf = new StringBuffer(array.length);
        int i;
        for (i = 0; i < array.length - 1; i++) {
            buf.append(array[i]);
            buf.append(',');
        }
        buf.append(array[i]);
        return buf.toString();
    }

    /**
     * Returns a comma delimited string representing the value of the array.
     */
    public static String toString(double[][] array) {
        StringBuffer buf = new StringBuffer();
        for (int k = 0; k < array.length; k++) {
            buf.append(toString(array[k]));
            buf.append(System.getProperty("line.separator"));
        }
        return buf.toString();
    }

    /**
     * Returns a comma delimited string representing the value of the array.
     */
    public static String toString(int[] array) {
        StringBuffer buf = new StringBuffer(array.length);
        int i;
        for (i = 0; i < array.length - 1; i++) {
            buf.append(array[i]);
            buf.append(',');
        }
        buf.append(array[i]);
        return buf.toString();
    }

    /**
     * Returns a comma delimited string representing the value of the array.
     */
    public static String toString(int[][] array) {
        StringBuffer buf = new StringBuffer();
        for (int k = 0; k < array.length; k++) {
            buf.append(toString(array[k]));
            buf.append(System.getProperty("line.separator"));
        }
        return buf.toString();
    }
}

Related Tutorials