Java Array Convert to toDoubleArray(final int[] intArray)

Here you can find the source of toDoubleArray(final int[] intArray)

Description

to Double Array

License

Open Source License

Declaration

public static double[] toDoubleArray(final int[] intArray) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.List;

public class Main {
    public static double[] toDoubleArray(final int[] intArray) {
        double[] array = null;
        if (intArray != null) {
            array = new double[intArray.length];
            for (int i = 0; i < intArray.length; i++) {
                array[i] = (double) intArray[i];
            }/*w w  w .  java2  s  .  c  o  m*/
        }
        return array;
    }

    public static double[] toDoubleArray(final String[] array) {
        double[] doubleArray = null;
        if (array != null) {
            doubleArray = new double[array.length];
            for (int i = 0; i < array.length; i++) {
                try {
                    doubleArray[i] = Double.parseDouble(array[i]);
                } catch (NumberFormatException e) {
                    doubleArray[i] = Double.NaN;
                }
            }
        }
        return doubleArray;
    }

    public static <E> double[] toDoubleArray(final E[] array) {
        double[] stringList = null;
        if (array != null) {
            stringList = new double[array.length];
            for (int i = 0; i < array.length; i++) {
                if (array[i] != null) {
                    try {
                        stringList[i] = Double.parseDouble(array[i].toString());
                    } catch (NumberFormatException nfe) {
                        stringList[i] = Double.NaN;
                    }
                } else {
                    stringList[i] = Double.NaN;
                }
            }
        }
        return stringList;
    }

    @Deprecated
    public static double[] toDoubleArray(final List<Double> list) {
        double[] array = new double[list.size()];
        for (int i = 0; i < list.size(); i++) {
            array[i] = list.get(i);
        }
        return array;
    }

    public static <E> String toString(final int[] list) {
        return toString(list, "\t");
    }

    public static <E> String toString(final int[] list, String separator) {
        StringBuilder sb = new StringBuilder();
        if (list != null && list.length > 0) {
            for (int i = 0; i < list.length - 1; i++) {
                sb.append(list[i]).append(separator);
            }
            sb.append(list[list.length - 1]);
        }
        return sb.toString();
    }

    public static <E> String toString(final double[] list) {
        return toString(list, "\t");
    }

    public static <E> String toString(final double[] list, String separator) {
        StringBuilder sb = new StringBuilder();
        if (list != null && list.length > 0) {
            for (int i = 0; i < list.length - 1; i++) {
                sb.append(list[i]).append(separator);
            }
            sb.append(list[list.length - 1]);
        }
        return sb.toString();
    }

    public static <E> String toString(final E[] list) {
        return toString(list, "\t");
    }

    public static <E> String toString(final E[] list, String separator) {
        StringBuilder sb = new StringBuilder();
        if (list != null && list.length > 0) {
            for (int i = 0; i < list.length - 1; i++) {
                if (list[i] != null) {
                    sb.append(list[i].toString()).append(separator);
                } else {
                    sb.append("null");
                }
            }
            if (list[list.length - 1] != null) {
                sb.append(list[list.length - 1].toString());
            } else {
                sb.append("null");
            }
        }
        return sb.toString();
    }
}

Related

  1. convertArrayType(String type)
  2. convertArrayTypeName(final String typeName)
  3. convertArrayValue(Object value, Class type)
  4. toDoubleArray(byte[] data)
  5. toDoubleArray(Double[] list)
  6. toFloatArray(final double[] doubles)
  7. toInt(Integer[] arr)
  8. toIntArray(double[] a)
  9. toIntArray(Integer[] data)