【问题标题】:How to convert a String ArrayList to an Integer ArrayList?如何将 String ArrayList 转换为 Integer ArrayList?
【发布时间】:2021-02-21 04:06:21
【问题描述】:
我正在尝试将字符串数组列表转换为整数数组列表,但它使我的应用程序崩溃,错误为NumberFormatException,请提供任何帮助。
List<String> sample = new ArrayList<String>(set2);
List<Integer> sample2 = new ArrayList<Integer>(sample.size());
for (String fav : sample) {
sample2.add(Integer.parseInt(fav));
}
【问题讨论】:
标签:
java
string
arraylist
parseint
【解决方案1】:
在此代码中,我添加了此代码fav.chars().allMatch( Character::isDigit ) 以检查字符串文本的所有字符是否为非数字,如果为真,则将其添加到sample2 数组中:
List<String> sample = new ArrayList<String>(set2);
List<Integer> sample2 = new ArrayList<Integer>(sample.size());
for (String fav : sample) {
if(fav.chars().allMatch( Character::isDigit )) {
sample2.add(Integer.parseInt(fav));
}
}
【解决方案2】:
你可以过滤掉那些包含非数字字符的字符串来避免这个错误:
List<String> sample = List.of("123", "235", "abc!", " ");
List<Integer> sample2 = sample.stream()
.filter(str -> str.codePoints()
// all characters are digits
.allMatch(Character::isDigit))
.map(Integer::parseInt)
.collect(Collectors.toList());
System.out.println(sample2); // [123, 235]
【解决方案3】:
每次添加到列表时都尝试此方法,以确保仅添加数字字符串(用 '0'-'9' 的字符表示的字符串。)
public static boolean isNumeric(String strNum) {
if (strNum == null) {
return false;
}
try {
double d = Double.parseDouble(strNum);
} catch (NumberFormatException nfe) {
return false;
}
return true;
}
所以你的代码看起来像:
List<String> sample = new ArrayList<String>(set2);
List<Integer> sample2 = new ArrayList<Integer>(sample.size());
for (String fav : sample) {
if (isNumeric(fav))
sample2.add(Integer.parseInt(fav));
}
这应该让你毫无例外地离开。
【解决方案4】:
根据你的代码:
-
第一件事是如何确定样本数组中的所有字符串都是整数。如果您确定它们是整数,那么您的代码就可以了。因为它是一个应用程序,所以您不需要它崩溃,所以在我看来正确地吞下异常是可以的(除非您正确记录什么
发生了)。
-
因此,如果可能的话,最好的方法是使用List<Integer> 列表(首先限制非整数值)。但是如果需要字符串,那么您可以先检查是否可以对其进行解析,然后将其添加到列表中,即是否只需要避免解析异常。希望这是有道理的。
方法 1(如果列表初始列表大小无关紧要,建议使用):
public static void main(String[] args){
List<String> sample=new ArrayList<>(Arrays.asList("1","2","non-integer")); //here the non-integer throws a number format exception but the try catch block catches it and returns 0;
List<Integer> sample2=new ArrayList<>(sample.size());
for (String fav:sample){
if ( isIntegerParseable(fav) ) {
sample2.add(Integer.parseInt(fav));
}
}
// sample2 output is : [1, 2]
}
private static boolean isIntegerParseable(String fav) {
try{
if(fav == null) return false;
Integer.parseInt(fav);
return true;
}catch (NumberFormatException nfe){
//logg this error ( the following value is not a number);
}
return false;
}
方法 2(不推荐):
public static void main(String[] args){
List<String> sample=new ArrayList<>(Arrays.asList("1","2","non-integer")); //here the non-integer throws a number format exception but the try catch block catches it and returns 0;
List<Integer> sample2=new ArrayList<>(sample.size());
for (String fav:sample){
sample2.add(toIntOrDefault(fav));
}
// "If you are returning a null then [1, 2, null] but returning null is even risky as it may result NPE"
// sample2 output is : [1, 2, 0]
}
private static int toIntOrDefault(String value){
try{
return Integer.parseInt(value);
}catch (NumberFormatException nfe){
//logg this error ( the following value is not a number);
// may throw your custom exceptions if any or default value in this case
}
return 0; //you can even return null and change return type to Integer as you are using List<Integer> which is an object type list.
}