gpt4 book ai didi

java - 非常小的代码优化

转载 作者:行者123 更新时间:2023-12-01 07:19:49 24 4
gpt4 key购买 nike

  while (i < a.size() && value2.substring(0, prefix.length()).compareTo(prefix) == 0) {
value2 = a.get(i);

if (value2.endsWith(suffix)) {
counter++;
setter = true;

}

i++;

}

我只是想知道是否有办法避免在我的代码中使用两次 get() 方法。我现在的问题是我需要之前分配值 2 以便我的 while 循环工作,但我还需要在 while 循环内更新它。

最佳答案

也许这就是您所需要的:

for (String value : a) {
if (!value.startWith(prefix)) break;
if (value.endsWith(suffix)) {
counter++;
setter = true;
}
}

如果 a 不可迭代:

        while(i < a.size()){
String value = a.get(i);
if (!value.startsWith(prefix))
break;
if(value.endsWith(suffix)){
counter++;
setter = true;
}
i++;
}

关于java - 非常小的代码优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42989321/

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