62,614
社区成员
发帖
与我相关
我的任务
分享
ArrayList <Integer> al = new ArrayList <Integer>();
@SuppressWarnings("unchecked")
加个这个也可以解决.引用 10 楼 cydp007 的回复:
小猴子 跟 大企鹅 老是一前一后啊.
抢分小分队啊...
import java.util.ArrayList;
import java.util.Iterator;
public class TestCollection {
@SuppressWarnings("unchecked")//加上这句就不会报错了..有时间把泛型好好看看..就可以了.
public static void main(String[] args) {
int b = 0;
ArrayList al = new ArrayList();
System.out.println("enter number:");
while (true) {
try {
b = System.in.read();
} catch (Exception e) {
System.out.println(e.getMessage());
}
if (b == '\r' || b == '\n')
break;
else {
int num = b - '0';
al.add(new Integer(num));
}
}
int sum = 0;
Iterator itr = al.iterator();
while (itr.hasNext()) {
Integer intObj = (Integer) itr.next();
sum += intObj.intValue();
}
System.out.println(sum);
}
}