当
operationIndex等于中的最后一个元素索引时
outputNum,
operationIndex +1则将大于最后一个元素索引:因此,您的异常
使用
for从索引1开始的循环并执行以下操作会更好:
// assuming that operationList and outputNum always // have the same number of elementsfor (int i = 1; i < operationList.size(); i++) { ... answer = outputNum.get(i - 1) * outputNum.get(i); ...}或者,也可以使用您的当前循环,但请执行以下操作:
// operationIndex++; // remove this and use .. if (++operationIndex >= outputNum.size()) break;



