线程“main”中的异常java.lang.IndexOutOfBoundsException [英] Exception in thread "main" java.lang.IndexOutOfBoundsException
本文介绍了线程“main”中的异常java.lang.IndexOutOfBoundsException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试获取特定像素兴趣点的rgb值
(< span class =code-keyword> int i = 0 ; i< = locations_original.size(); i ++)
{
System.out.println( x,y: + locations_original。 get(i).x + , +
locations_original.get(i)。 Y);
Double x = locations_original.get(i).x;
Double y = locations_original.get(i).y;
int pixel = image.getRGB(x.intValue(),y.intValue());
printPixelARGB(pixel);
}
public static void printPixelARGB( int pixel){
int alpha =(pixel>> 24 )& 0xFF的;
int red =(pixel>> 16 )& 0xFF的;
int green =(pixel>> 8 )& 0xFF的;
int blue =(pixel)& 0xFF的;
System.out.println( argb: + alpha + , + red + , + green + ,
+ blue );
}
但此行中有错误
System.out.println( x,y: + locations_original.get (i).x + , + locations_original.get(i).y);
引用:如下
线程中的异常 main java.lang.IndexOutOfBoundsException :索引: 8 ,大小: 8
at java.util.ArrayList.rangeCheck(Unknown来源)
at java.util.ArrayList.get(未知来源)
at boofcv.ExampleInterestPoint.main(ExampleInterestPoint.java: 555 )
我该如何解决这个问题?
问候!
解决方案
最有可能的是,
( int i = 0 ; i< = locations_original.size(); i ++)
应替换为
for ( int i = 0 ; i< locations_original.size(); i ++)
我甚至要解释原因吗?这是小学数学;大多数开发人员从第一眼看到这个错误,在反应水平,因为正确的代码是一个非常常见的模式:N个元素的范围从0到N-1索引。
-SA
循环初始化出现问题....
它应该像....
for ( int i = 0 ; i< locations_original.size(); i ++){...}
I am trying to get rgb values for specific pixels "interest points"
for (int i = 0; i <=locations_original.size();i++)
{
System.out.println("x,y: " + locations_original.get(i).x + ", " +
locations_original.get(i).y);
Double x=locations_original.get(i).x;
Double y=locations_original.get(i).y;
int pixel = image.getRGB(x.intValue(),y.intValue());
printPixelARGB(pixel);
}
public static void printPixelARGB(int pixel) {
int alpha = (pixel >> 24) & 0xff;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;
System.out.println("argb: " + alpha + ", " + red + ", " + green + ",
" + blue);
}
but there is an error in this line
System.out.println("x,y: " + locations_original.get(i).x + ", " +locations_original.get(i).y);
Quote:as following
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 8, Size: 8 at java.util.ArrayList.rangeCheck(Unknown Source) at java.util.ArrayList.get(Unknown Source) at boofcv.ExampleInterestPoint.main(ExampleInterestPoint.java:555)
How can I fix this?
Regards!
解决方案
Most likely, the line
for (int i = 0; i <= locations_original.size(); i++)
should be replaced with
for (int i = 0; i < locations_original.size(); i++)
Do I even have to explain why? This is elementary mathematics; and most developers catch this bug from the first glance, at the level of reflexes, because right code is a very common pattern: a range of N elements is indexed from 0 to N−1.
—SA
there is a problem in your loop initialization ....
it should be like ....
for (int i = 0; i <locations_original.size();i++){...}
这篇关于线程“main”中的异常java.lang.IndexOutOfBoundsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文