gpt4 book ai didi

java - 如何在最后一个foreach项目中添加字符串?

转载 作者:行者123 更新时间:2023-12-01 22:03:41 24 4
gpt4 key购买 nike

我想在 foreach 数组的最后一项中附加某个字符串。

该程序运行得非常好。给定“待处理”数组中的项目,它应该将输出值附加到待处理值的最后一项中:

String a = out + "-" + rptdate + "-";
for (String pn : pending) {
//checks if total value + pending length value is less than 160
if (a.length() + pn.length() < 160) { // < to leave room for the comma as well
if (a.length() != 0) {
if (a.length() != 14) {
a += ",";
}
}

a += pn;
} else {
resultingStrings.add(a);
a = pn;
Log.d("messages", a);
}
}
resultingStrings.add(a);
for (String r : resultingStrings) {
sendSMS(r);
}

最佳答案

尝试简单的代码

int size = pending.size();
int index = 0;
for (String pn : pending) {
if(index == size - 1){
// it is last foreach => add your last string here
}
index++;
...
}

希望这有帮助

关于java - 如何在最后一个foreach项目中添加字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33205121/

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