Java IndexOutofBoundSexException-只是有时候?

Java IndexOutofBoundSexException-只是有时候?,java,loops,for-loop,indexing,Java,Loops,For Loop,Indexing,我的程序中不断出现随机的java.lang.IndexOutOfBoundsException错误。 我做错了什么? 程序运行得很好,这是一个很长的for循环,但是对于一些元素,我似乎得到了这个错误,然后它继续到下一个元素,它工作得很好 for (int i = 0; i < response.getSegments().getSegmentInfo().size()-1; i++) { reservedSeats = response.getSegments().getSegmen

我的程序中不断出现随机的java.lang.IndexOutOfBoundsException错误。 我做错了什么? 程序运行得很好,这是一个很长的for循环,但是对于一些元素,我似乎得到了这个错误,然后它继续到下一个元素,它工作得很好

for (int i = 0; i < response.getSegments().getSegmentInfo().size()-1; i++) {
   reservedSeats = response.getSegments().getSegmentInfo().get(i).getCabinSummary().getCabinClass().get(i).getAmountOfResSeat();
   usedSeats = response.getSegments().getSegmentInfo().get(i).getCabinSummary().getCabinClass().get(i).getAmountOfUsedSeat();
   System.out.println("Reserved Seats: " + reservedSeats);
   System.out.println("Used Seats    : " + usedSeats);
}
for(int i=0;i

如何防止出现此错误?

您似乎在使用
i
将索引分为两个完全独立的
列表
对象:

response.getSegments().getSegmentInfo().get(i) // indexing into response.getSegments().getSegmentInfo()
.getCabinSummary().getCabinClass().get(i) // indexing into getCabinSummary().getCabinClass()
.getAmountOfResSeat();

我觉得这是不对的。这应该是这样发生的吗?由
getCabinClass()
返回的列表是否保证至少与由
getSegmentInfo()
返回的列表一样长?

您似乎在使用
i
索引到两个完全独立的
list
对象:

response.getSegments().getSegmentInfo().get(i) // indexing into response.getSegments().getSegmentInfo()
.getCabinSummary().getCabinClass().get(i) // indexing into getCabinSummary().getCabinClass()
.getAmountOfResSeat();

我觉得这是不对的。这应该是这样发生的吗?由
getCabinClass()
返回的列表是否保证至少与由
getSegmentInfo()
返回的列表一样长?

假设
response.getSegments().getSegmentInfo()
总是返回一个大小相同的数组,并在给定循环头的情况下对其调用
.get(i)
(但是您是否知道您正在跳过最后一个元素?)但是,您是否确定
.getCabinSummary()
将返回一个与
getSegmentInfo()
数组一样大的数组?您使用
i
在两个不同的数组中执行查找看起来很可疑

您可以将循环体中的第一行拆分为两个单独的行(这里我只是猜测类型名称):

List segmentInfo=response.getSegments().getSegmentInfo().get(i);
reservedSeats=segmentInfo.getCabinSummary().get(i.GetAmountFresSeat();

然后,您将看到是哪个查找导致了崩溃。

假设
response.getSegments().getSegmentInfo()
总是返回一个大小相同的数组,调用
.get(i)
应该是安全的,给定循环头(但是您知道您正在跳过最后一个元素吗?)。但是,您确定
.getCabinSummary()
将返回一个与
getSegmentInfo()
数组一样大的数组?您使用
i
在两个不同的数组中执行查找,这看起来很可疑

您可以将循环体中的第一行拆分为两个单独的行(这里我只是猜测类型名称):

List segmentInfo=response.getSegments().getSegmentInfo().get(i);
reservedSeats=segmentInfo.getCabinSummary().get(i.GetAmountFresSeat();

然后,您将看到哪个查找导致了崩溃。

您正在使用
i
作为区段信息列表和客舱类别列表的索引。这似乎是问题的根源

我不知道你的域模型,但我想我们这里需要两个不同的计数器


重构代码以显示问题(猜测类型,替换为正确的类名)

List segmentInfos=response.getSegments().getSegmentInfo();
对于(int i=0;i
您正在使用
i
作为区段信息列表和客舱等级列表的索引。这似乎是您问题的根源

我不知道你的域模型,但我想我们这里需要两个不同的计数器


重构代码以显示问题(猜测类型,替换为正确的类名)

List segmentInfos=response.getSegments().getSegmentInfo();
对于(int i=0;i
对于那些认为这是一个数组的人来说,它更像是一个列表

让我猜猜,您曾经得到ConcurrentModificationException,因此您重写了循环以使用元素的索引查找(避免使用迭代器)。祝贺您,您修复了异常,但没有解决问题

此循环运行时,您正在更改列表。您不时会删除一个元素。您不时会查看最后一个元素
size()-1
。操作顺序如下所示:

 (some thread)
 remove an element from response.getSegments().getSegmentInfo()
 (some possibly other thread)
 lookup up the size()-1 element of the above
您访问一个不再存在的元素,并将引发IndexOutOfBoundsException

您需要通过控制对该列表的访问来修复该列表的逻辑,这样,如果您需要检查所有元素,您就不会假设该列表在跨越所有元素时是相同的,或者(更好的解决方案)冻结循环的列表

执行后一种操作的简单方法是复制列表(但不是列表的元素)并在t上迭代
 (some thread)
 remove an element from response.getSegments().getSegmentInfo()
 (some possibly other thread)
 lookup up the size()-1 element of the above