线程“main”中的异常java.lang.IndexOutOfBoundsException

编程入门 行业动态 更新时间:2024-10-19 02:24:51
本文介绍了线程“main”中的异常java.lang.IndexOutOfBoundsException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试获取特定像素兴趣点的rgb值

( 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

本文发布于:2023-11-22 20:33:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618918.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   异常   main   IndexOutOfBoundsException   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!