比较数据的例外情况

时间:2012-10-16 10:49:31

标签: java exception loops

使索引超出

的绑定异常
    for (int recordData = 0; recordData < recordDataList.size(); recordData++)
    {
        RecordData nextRecordData = recordDataList.get(recordData + 1);
        if (nextRecordData.getRespondentId() !=     
            recordDataList.get(recordData).getRespondentId()) {
            rowDataNumber++;
        }
    }

这里的尺寸是3.但是我需要将第三个数据与第二个数据进行比较。 我修改了我的喜欢

    for (int i = 1; i < recordDataList.size(); i++) {
                         RecordData recordData = recordDataList.get(i - 1);
                RecordData nextRecordData = recordDataList.get(i);
                         commentData = recordData.getCommentText();
if (nextRecordData.getRespondentId() != recordData
                        .getRespondentId()) {
                    rowDataNumber++;
                }
}

大小为3我只得到commentData中的前两个数据,第三个数据没有显示。

1 个答案:

答案 0 :(得分:3)

当您将值与下一个值进行比较时,需要有一个下一个值。

您需要使用

for (int i = 0; i < recordDataList.size() - 1; i++) {
    RecordData recordData = recordDataList.get(i);
    RecordData nextRecordData = recordDataList.get(i + 1);

for (int i = 1; i < recordDataList.size(); i++) {
    RecordData recordData = recordDataList.get(i - 1);
    RecordData nextRecordData = recordDataList.get(i);