0

使索引超出范围异常

    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++;
                }
}

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

4

1 回答 1

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);
于 2012-10-16T10:52:51.420 回答