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?

huangapple go评论147阅读模式
英文:

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&#39;ve checked my iterative loops and can&#39;t really see anything wrong with it (I&#39;m sure that I&#39;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&#39;m trying to traverse over is just too big?


public String filterOrder() throws SQLException{	
	
	ArrayList&lt;OrderDetails&gt;od= this.getOrderDetails(&quot;orderdetails&quot;);
	ArrayList&lt;orders&gt;ord=this.getOrders();
	Map&lt;Integer,Double&gt;map=new HashMap&lt;Integer,Double&gt;();
	StringBuffer filter=new StringBuffer();
	
	
	for(int i=0;i&lt;=ord.size();i++) {
		double tmp=0;
		for(int j=0; j&lt;=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&lt;Integer, Double&gt; entry: map.entrySet()) {
			if(entry.getValue()&gt;5000) {
				filter.append(entry.getKey()+&quot; has a total value of : $ &quot;+Math.round(entry.getValue()*100)/100.0+&quot;\n&quot;);
			}
			
		}
		
		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 &quot;orderdetails&quot; contains 5990 rows, so the arraylist &quot;od&quot; contains 5590 elements. The table &quot;orders&quot; contains 650 rows, so the arraylist &quot;ord&quot; 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&#39;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()给你的是比允许的最大索引大一的值。在条件语句中,你应该使用&lt;而不是&lt;=。想象一下一个只有一个项的列表:尺寸是1,但在索引0处只有一个项目,在索引1处没有任何内容。

<details>
<summary>英文:</summary>

size() gives you one greater than the maximum allowed index. You should use &lt; not &lt;= 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>



huangapple
  • 本文由 发表于 2020年5月3日 17:59:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/61572612.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定