简体   繁体   English

在Java中使用While循环对列表中的元素进行计数

[英]Count elements of a list using While loop in java

I am passing some parameters in the URL and then I add them in a list. 我在URL中传递了一些参数,然后将它们添加到列表中。 My list has a limit of 5 elements. 我的列表限制为5个元素。 So if someone adds 6th element in the URL the list would simply ignore it. 因此,如果有人在URL中添加第6个元素,则列表将完全忽略它。 So I am trying to use a counter but the logic is not working as desired. 因此,我尝试使用计数器,但逻辑未按预期工作。 I am using While loop to achieve this. 我正在使用While循环来实现此目的。 So if list size is smaller than 5 set the agencyCds otherwise just return the list. 因此,如果列表大小小于5,请设置agencyCds,否则只需返回列表即可。

private List<IUiIntegrationDto> generateViewIntegrationReportData(ESignatureIntegrationConfig eSignConfig) throws Exception {
    int counter = 1;
    if(eSignConfig.getAdditionalAgencyCds() != null ) {

        List<String> combinedAgencyCds = new ArrayList<String>();

        for(String agencyCd : eSignConfig.getAgencyCd()) {
            combinedAgencyCds.add(agencyCd);
        }

        StringTokenizer token = new StringTokenizer(eSignConfig.getAdditionalAgencyCds().toString(), StringConstants.COMMA);
        while(token.hasMoreTokens()) {
            combinedAgencyCds.add(token.nextToken());
        }

        while(combinedAgencyCds.size() < 5) {
            counter = counter + 1;
            eSignConfig.setAgencyCd(combinedAgencyCds);
        }

    //  eSignConfig.setAgencyCd(combinedAgencyCds);
    } 
    List<IUiIntegrationDto> intgList = getUiIntegrationManager().retrieveUiIntegrationReportData(eSignConfig.getAgencyCd(), eSignConfig.getCreatedDays(),
            eSignConfig.getLob(), eSignConfig.getTransactionStatus(), eSignConfig.getAccounts(), eSignConfig.getSortKey(), eSignConfig.getSortOrder());

    return intgList;
}

I am not completely sure about this logic if it is correct or if there is nay better approach. 我对这种逻辑是否正确或是否有更好的方法还不确定。

Thanks 谢谢

Try this instead of the last while in your code: 尝试使用此代码,而不是最后一个代码:

if(combinedAgencyCds.size() <= 5) {
   eSignConfig.setAgencyCd(combinedAgencyCds); 
} else {
   eSignConfig.setAgencyCd(combinedAgencyCds.subList(0, 5));
}

The full combined list will then be used if it is less than 5 in size. 如果大小小于5,则将使用完整的合并列表。 Otherwise, only the first 5 elements are used. 否则,仅使用前5个元素。

Edit: Or even better: 编辑:甚至更好:

eSignConfig.setAgencyCd(combinedAgencyCds.subList(0, Math.min(5, combinedAgencyCds.size())));

Ok so let's break down what your code is currently doing. 好的,让我们分解一下您的代码当前正在做什么。

int counter = 1;

while(combinedAgencyCds.size() < 5) {
    counter = counter + 1;
    eSignConfig.setAgencyCd(combinedAgencyCds);
}

This snippet of code has a couple things wrong best I can tell. 我可以说,这段代码有几处错误。 First, this loop has the possibility of running forever or not at all. 首先,此循环可能永远运行或根本不运行。 Because combinedAgencyCds is never being manipulated, the size won't ever change and the logic being checked in the while loop never does anything. 因为从未操作combinedAgencyCds ,所以大小永远不会改变,并且在while循环中检查的逻辑不会做任何事情。 Second, there's a more efficient loop for doing this, assuming you don't need the counter variable outside of its usage in the while loop and that is using for loops. 其次,假设您在while循环中不需要使用counter变量,而使用for循环,则有一个更高效的循环可以执行此操作。

Example syntax is as follows: 示例语法如下:

for (int i = 0; i < combinedAgencyCds.size(); i++) {
    if (i < 5) {        
        // Do your logic here.
    }
    else {
        break; // Or handle extra values however you want.
    }
}

Notice there is no need for the explicit declaration for a counter variable as "i" counts for you. 请注意,由于“ i”对您很重要,因此无需显式声明计数器变量。

Now in your actual logic in the loop, I'm not sure what the setAgencyCd method does, but if it simply sets a list variable in the eSignConfig like it appears to, repeating it over and over isn't going to do anything. 现在,在循环的实际逻辑中,我不确定setAgencyCd方法的作用,但是如果它只是像看起来那样在eSignConfig设置一个列表变量,则eSignConfig重复将不会做任何事情。 From what I can see in your code, you are setting a variable with the same value 5 times. 从您的代码中可以看到,您正在将相同值的变量设置为5次。 If you need any more explanation just let me know and I will be happy to revise the answer. 如果您需要更多说明,请告诉我,我们将很乐意修改答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM