gpt4 book ai didi

java - 比较数据的异常(exception)情况

转载 作者:行者123 更新时间:2023-12-02 00:10:26 28 4
gpt4 key购买 nike

获取索引越界异常

    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 中获取前两个数据,第三个数据未显示。

最佳答案

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

您需要使用

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);

关于java - 比较数据的异常(exception)情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12913042/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com