java - 如何将迭代计数值设置为java中的一个字段
问题描述
我有一个通过 for-each 循环迭代的列表。所以我想要的是一旦值设置为设置customerAttemptTemp.setIsActive("N") customerAttemptTemp.setAttemptCount();
值应该是 1。
最后 customerAttemptList 列表的大小为 5 的尝试计数需要为 5。
这是代码段。
List<CustomerAttempt> customerAttemptList = customerAttemptService.findCustomerAttempts(customerAttemptWrong, _serviceContext);
for (CustomerAttempt customerAttemptTemp : customerAttemptList) {
customerAttemptTemp.setIsActive("N");
customerAttemptTemp.setIsActive( // **Where the count need to be updated**);
customerAttemptService.update(customerAttemptTemp, _serviceContext);
}
解决方案
count
您可以通过在循环外引入一个变量来做到这一点,然后在每次迭代后增加它
int count = 1;
for(CustomerAttempt customerAttemptTemp : customerAttemptList) {
customerAttemptTemp.setIsActive("N");
customerAttemptTemp.setIsActive(count++);
customerAttemptService.update(customerAttemptTemp, _serviceContext);
}