Java Integer Create toIntArray(final double[] doubleArray)

Here you can find the source of toIntArray(final double[] doubleArray)

Description

to Int Array

License

Open Source License

Declaration

public static int[] toIntArray(final double[] doubleArray) 

Method Source Code

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

public class Main {
    public static int[] toIntArray(final double[] doubleArray) {
        int[] dataToInt = null;
        if (doubleArray != null) {
            dataToInt = new int[doubleArray.length];
            for (int i = 0; i < doubleArray.length; i++) {
                dataToInt[i] = (int) Math.round(doubleArray[i]);
            }/*www  .j  a  va2  s.  c  o  m*/
        }
        return dataToInt;
    }

    public static int[] toIntArray(final String[] array, int defaultValue) {
        int[] intArray = null;
        if (array != null) {
            intArray = new int[array.length];
            for (int i = 0; i < array.length; i++) {
                if (array[i] != null) {
                    try {
                        intArray[i] = Integer.parseInt(array[i].toString());
                    } catch (NumberFormatException nfe) {
                        intArray[i] = defaultValue;
                    }
                } else {
                    intArray[i] = defaultValue;
                }
            }
        }
        return intArray;
    }

    public static <E> int[] toIntArray(final E[] array, int defaultValue) {
        int[] stringList = null;
        if (array != null) {
            stringList = new int[array.length];
            for (int i = 0; i < array.length; i++) {
                if (array[i] != null) {
                    try {
                        stringList[i] = Integer.parseInt(array[i].toString());
                    } catch (NumberFormatException nfe) {
                        stringList[i] = defaultValue;
                    }
                } else {
                    stringList[i] = defaultValue;
                }
            }
        }
        return stringList;
    }

    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. toIntArray(byte[] input)
  2. toIntArray(double[] a)
  3. toIntArray(double[] array)
  4. toIntArray(final byte[] array)
  5. toIntArray(final byte[] bytes)
  6. toIntArray(final int ip)
  7. toIntArray(final Object[] array)
  8. toIntArray(final String[] arrayOfIntStrings)
  9. toIntArray(int... values)