起初我的代码是这样写的,然后就报出来一个异常
: Index 3 out of bounds for length 3
for(int i=0;i<=arrayList.size();i++) {
Goods thing = (Goods) arrayList.get(i);
System.out.println(thing.getId()+","+thing.getName()+","+thing.getPrice());
}
到最后发现,只要把i<=()中的 = 删除即可,而且这样的才真正的把集合中的元素全部遍历完
for(int i=0;i<arrayList.size();i++) {
Goods thing = (Goods) arrayList.get(i);
System.out.println(thing.getId()+","+thing.getName()+","+thing.getPrice());
}
尤其注意: = 的存在哦!