英文:
Can you get IndexOutOfBoundException when the array list you are trying to iterate over is too large? Or is there something wrong with my loops?
问题
我尝试在一个包含数千个条目的ArrayList上进行迭代。但是我却收到了IndexOutOfBoundsException异常。我已经检查了我的迭代循环,并没有发现任何问题(我确定我没有试图访问ArrayList之外的内容)。我的嵌套for循环有问题吗,还是我要遍历的ArrayList太大了?
公开的代码部分已被翻译,如下所示:
public String filterOrder() throws SQLException{
ArrayList<OrderDetails> od = this.getOrderDetails("orderdetails");
ArrayList<orders> ord = this.getOrders();
Map<Integer, Double> map = new HashMap<Integer, Double>();
StringBuffer filter = new StringBuffer();
for(int i=0; i<=ord.size(); i++) {
double tmp = 0;
for(int j=0; j<=od.size(); j++) {
if(ord.get(i).getOrderNumber() == od.get(j).getOrderNumber()) {
tmp = tmp + (od.get(j).getPriceEach() * od.get(j).getQuantityOrdered());
}
map.put(ord.get(i).getOrderNumber(), tmp);
}
}
for(Entry<Integer, Double> entry: map.entrySet()) {
if(entry.getValue() > 5000) {
filter.append(entry.getKey() + "的总价值为:$ " + Math.round(entry.getValue() * 100) / 100.0 + "\n");
}
}
return filter.toString();
}
关于数组列表(od和ord)的背景:我使用的这两个ArrayList(od和ord)包含来自两个SQL表的对象,每个对象存储在它们各自的表中对应的一行/元组。表"orderdetails"包含5990行,因此ArrayList "od" 包含5590个元素。表"orders"包含650行,因此ArrayList "ord"包含650个元素。当我提前终止循环(在数组列表的最后一个条目之前结束迭代),我得到了一些预期的结果。只是当我遍历整个数组列表时,我会收到异常。
我从控制台得到的异常消息如下所示:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2996, Size: 2996
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at com1028_coursework_sl01586.BaseQuery.filterOrder(BaseQuery.java:150)
at com1028_coursework_sl01586.QueryTest.main(QueryTest.java:34)
问题可能是什么?是我的循环还是ArrayList太大了?
<details>
<summary>英文:</summary>
I am trying to iterate over an arraylist with a least a couple thousands of entries in it. But I was given an IndexOutOfBoundEcxception instead. I've checked my iterative loops and can't really see anything wrong with it (I'm sure that I'm not trying to reach anything outside of the arraylist). Is there something wrong with my nested for loop or is it that the arraylist I'm trying to traverse over is just too big?
public String filterOrder() throws SQLException{
ArrayList<OrderDetails>od= this.getOrderDetails("orderdetails");
ArrayList<orders>ord=this.getOrders();
Map<Integer,Double>map=new HashMap<Integer,Double>();
StringBuffer filter=new StringBuffer();
for(int i=0;i<=ord.size();i++) {
double tmp=0;
for(int j=0; j<=od.size();j++) {
if(ord.get(i).getOrderNumber()==od.get(j).getOrderNumber()) {
tmp=tmp+(od.get(j).getPriceEach()*od.get(j).getQuantityOrdered());
}
map.put(ord.get(i).getOrderNumber(), tmp);
}
}
for(Entry<Integer, Double> entry: map.entrySet()) {
if(entry.getValue()>5000) {
filter.append(entry.getKey()+" has a total value of : $ "+Math.round(entry.getValue()*100)/100.0+"\n");
}
}
return filter.toString();
}
context: The two arraylists I used (od and ord) contains objects from two SQL tables, and each objects stored within each of them corresponds to one row/tuple from their respective table. The table "orderdetails" contains 5990 rows, so the arraylist "od" contains 5590 elements. The table "orders" contains 650 rows, so the arraylist "ord" contains 650 elements. When I terminate the loops prematurely(setting the iteration to end way before the last entry of the arraylist), some of the result I expected did come out. It's just that when I iterate over the entire arraylist I get the exception.
The exception message I got from my console goes as follow:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2996, Size: 2996
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at com1028_coursework_sl01586.BaseQuery.filterOrder(BaseQuery.java:150)
at com1028_coursework_sl01586.QueryTest.main(QueryTest.java:34)
What could be the problem? My loops or the arraylist being too big?
</details>
# 答案1
**得分**: 2
size()给你的是比允许的最大索引大一的值。在条件语句中,你应该使用<而不是<=。想象一下一个只有一个项的列表:尺寸是1,但在索引0处只有一个项目,在索引1处没有任何内容。
<details>
<summary>英文:</summary>
size() gives you one greater than the maximum allowed index. You should use < not <= in your conditions. Think about a list with one item in it: the size is 1 but there is only an item at index 0, and nothing at index 1.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论