这个是JDK 1.5 的新特性。举个例子。 一个列表中加入3个数字,然后从列表中取出作合计的操作。JDK 1.4 的时候 List testList = new ArrayList(); testList.add(new Integer(100)); testList.add(new Integer(200)); testList.add(new Integer(300)); int result = 0; for(int i = 0; i < testList.size(); i ++) { // 这里从列表中取数据时,需要强制转换。 result += (Integer)testList.get(i).intValue(); } System.out.println(result);JDK 1.5 的时候 List<Integer> testList = new ArrayList<Integer>(); // 注意,JDK1.5的 int 的 100,能够自动的转换成 Integer 类型。不强制要求 new Integer(100) testList.add(100); testList.add(200); testList.add(300); int result = 0; for(int i = 0; i < testList.size(); i ++) { // 这里从列表中取数据时,不需要强制转换。 result += testList.get(i).intValue(); } System.out.println(result); PS. 以上代码没有编译运行测试过,仅仅用于说明。
List testList = new ArrayList();
testList.add(new Integer(100));
testList.add(new Integer(200));
testList.add(new Integer(300));
int result = 0;
for(int i = 0; i < testList.size(); i ++) {
// 这里从列表中取数据时,需要强制转换。
result += (Integer)testList.get(i).intValue();
}
System.out.println(result);JDK 1.5 的时候
List<Integer> testList = new ArrayList<Integer>();
// 注意,JDK1.5的 int 的 100,能够自动的转换成 Integer 类型。不强制要求 new Integer(100)
testList.add(100);
testList.add(200);
testList.add(300);
int result = 0;
for(int i = 0; i < testList.size(); i ++) {
// 这里从列表中取数据时,不需要强制转换。
result += testList.get(i).intValue();
}
System.out.println(result);
PS. 以上代码没有编译运行测试过,仅仅用于说明。
------------------------------
学习了!