Here you can find the source of toIntegerList(List
Parameter | Description |
---|---|
strList | can't be null |
failOnException | if an element can not be parsed should we return null or add a null element to the list. |
public static List<Integer> toIntegerList(List<String> strList, boolean failOnException)
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; import java.util.List; public class Main { /**// w w w. java 2s .c om * Parse a list of String into a list of Integer. * If one element can not be parsed, the behavior depends on the value of failOnException. * @param strList can't be null * @param failOnException if an element can not be parsed should we return null or add a null element to the list. * @return list of all String parsed as Integer or null if failOnException */ public static List<Integer> toIntegerList(List<String> strList, boolean failOnException) { List<Integer> intList = new ArrayList<Integer>(); for (String str : strList) { try { intList.add(Integer.parseInt(str)); } catch (NumberFormatException nfe) { if (failOnException) { return null; } else { intList.add(null); } } } return intList; } }