起初我的代码是这样写的,然后就报出来一个异常

java.lang.IndexOutOfBoundsException: 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<=arrayList.size()中的 = 删除即可,而且这样的才真正的把集合中的元素全部遍历完

for(int i=0;i<arrayList.size();i++) {
			Goods thing = (Goods) arrayList.get(i);
			System.out.println(thing.getId()+","+thing.getName()+","+thing.getPrice());
		}

尤其注意: = 的存在哦!

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐