本文介绍了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 62Also 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异常
发布评论