直接贴代码:
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.ArrayUtils;
public class StringToIntArray {
public static void main(String[] args) {
//输入
String str = "1,2,3,4,5,a,6,7,8,b,9";
String[] arr = str.split(",");
List<Integer> list = new ArrayList<Integer>();
if(arr != null && arr.length > 0){
for(int i=0;i<arr.length;i++){
try {
list.add(Integer.valueOf(arr[i]));
} catch (NumberFormatException e) {
//e.printStackTrace();
}
}
}
Integer[] integerArr = list.toArray(new Integer[list.size()]);
int[] result = ArrayUtils.toPrimitive(integerArr);
//打印结果
if(result != null && result.length > 0){
for(int i=0;i<result.length;i++){
System.out.println(result[i]);
}
}
}
}