如何将迭代计数值设置为Java

时间:2019-02-19 07:13:24

标签: java count iteration

我有一个通过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);
      }

2 个答案:

答案 0 :(得分:1)

您可以通过在循环外引入count变量,然后在每次迭代后增加变量来实现此目的

int count = 1;
for(CustomerAttempt customerAttemptTemp : customerAttemptList) {
    customerAttemptTemp.setIsActive("N");
    customerAttemptTemp.setIsActive(count++);
    customerAttemptService.update(customerAttemptTemp, _serviceContext);
}

答案 1 :(得分:0)

尝试一下。

for (int count = 0 ; count < customerAttemptList.size(); count++) {
    customerAttemptList.get(count).setIsActive("N");
    customerAttemptList.get(count).setIsActive(count + 1);
    customerAttemptService.update(customerAttemptList.get(count), _serviceContext);
}