import java.util.ArrayList;
import java.util.Iterator;
public class Integer2String {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
String str = "12341";
// String str="122abc"
try {
list.add(Integer.parseInt(str));
} catch (NumberFormatException e) {
e.printStackTrace();
System.out.println("数字格式化异常,就是字符串不是合法的十进制整数");
}
Iterator<Integer> it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
// System.out.println(list);
}
}
一开始我以为是这样,后来看了反射才知道,没这么简单,应该用反射做才对。
import java.lang.reflect.Method;
import java.util.ArrayList;
public class Test1 {
public static void main(String[] args) throws Exception {
ArrayList<Integer> al = new ArrayList<Integer>();
Method method = al.getClass().getMethod("add", Object.class);
method.invoke(al, "abclad5f4as6");
System.out.println(al);
}
}