Java List to Int List toIntegerList(final E[] array)

Here you can find the source of toIntegerList(final E[] array)

Description

to Integer List

License

Open Source License

Declaration

public static <E> List<Integer> toIntegerList(final E[] array) 

Method Source Code

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

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static <E> List<Integer> toIntegerList(final E[] array) {
        List<Integer> integerList = null;
        if (array != null) {
            integerList = new ArrayList<Integer>(array.length);
            for (E e : array) {
                if (e != null) {
                    try {
                        integerList.add(Integer.parseInt(e.toString()));
                    } catch (NumberFormatException nfe) {
                        integerList.add(null);
                    }/* w ww  . j a  va  2s  .c  om*/
                } else {
                    integerList.add(null);
                }
            }
        }
        return integerList;
    }

    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(List values)
  2. toIntArray_impl(List i_list, int i_offset, int i_size)
  3. toIntegerArray(final List list)
  4. toIntegerArray(List arrayValues)
  5. toIntegerArray(List values)
  6. toIntegerList(int... array)
  7. toIntegerList(int[] array)
  8. toIntegerList(int[] intArray)
  9. toIntegerList(List strList, boolean failOnException)