IndexOutOfBoundsException异常

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

Hi 这段代码将返回 indexoutofboundsException ,真的不知道为什么? 我想从 pointlist 中删除​​那些与列表中的对象相同的对象。

public void listOfExternalPoints(List< Point> list){ System.out.println(list.size() ); System.out.println(pointList.size()); int n = pointList.size(); (int i = pointList.size() - 1; i> = 0; i--){ for(int j = 0; j< list.size(); j ++) { if(pointList.get(i)==(list.get(j))){ pointList.remove(i); n--; } } } }

另外,println的输出将是:

54 62

另外例外:

异常在线程AWT-EventQueue-0java.lang.IndexOutOfBoundsException:索引:60,大小:60 在java.util.ArrayList.RangeCheck(ArrayList.java:547)在java.util。 ArrayList.get(ArrayList.java:322)在ConvexHull.BlindVersion.listOfExternalPoints(BlindVersion.java:83)

谢谢。

解决方案

嘿,你从列表中删除了一些元素。所以列表比循环开始时更小。

我建议你使用:

pointList.removeAll(list)

或一个迭代器。 / p>

Hi this code will return indexoutofboundsException and really I don't know why? I want to remove those objects from pointlist which are as the same as an object in the list.

public void listOfExternalPoints(List<Point> list) { System.out.println(list.size()); System.out.println(pointList.size()); int n = pointList.size(); for (int i = pointList.size() - 1; i >= 0; i--) { for (int j = 0; j < list.size(); j++) { if (pointList.get(i)==(list.get(j))) { pointList.remove(i); n--; } } } }

Also the out put of println will be :

54 62

Also the exception:

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 60, Size: 60 at java.util.ArrayList.RangeCheck(ArrayList.java:547) at java.util.ArrayList.get(ArrayList.java:322) at ConvexHull.BlindVersion.listOfExternalPoints(BlindVersion.java:83)

thanks.

解决方案

hey, you removed some elements from the list. So the list is smaller than it was at the beginning of the loop.

I suggest you to use:

pointList.removeAll(list)

Or an iterator.

更多推荐

IndexOutOfBoundsException异常

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

发布评论

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

>www.elefans.com

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