Java List to Int List toIntArray(final List list)

Here you can find the source of toIntArray(final List list)

Description

to Int Array

License

Open Source License

Declaration

public static <E> int[] toIntArray(final List<E> list) 

Method Source Code

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

import java.util.*;

public class Main {
    public static <E> int[] toIntArray(final List<E> list) {
        return toIntArray(list, -1);
    }/*from w  w  w.j a  v a  2 s . c  om*/

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

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

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

Related

  1. listToIntArray(final List ints)
  2. listToIntArray(List l)
  3. listToIntArray(List list)
  4. listToIntegerArray(List list)
  5. toIntArray(final List numbers)
  6. toIntArray(final List list)
  7. toIntArray(final List list)
  8. toIntArray(java.util.List list)
  9. toIntArray(List list)