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

Java 如何在最后一个foreach项中添加字符串?,java,android,string,foreach,Java,Android,String,Foreach,我想在foreach数组的最后一项中附加某些字符串 这个程序运行得非常好。给定“挂起”数组中的项,它应在挂起值的最后一项中追加out值: 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) { //

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

这个程序运行得非常好。给定“挂起”数组中的项,它应在挂起值的最后一项中追加out值:

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);
}
字符串a=out+“-”+rptdate+“-”;
用于(字符串pn:挂起){
//检查总值+挂起长度值是否小于160
如果(a.length()+pn.length()<160){/<也为逗号留出空间
如果(a.长度()!=0){
如果(a.长度()!=14){
a+=“,”;
}
}
a+=pn;
}否则{
结果字符串。添加(a);
a=pn;
日志d(“消息”,a);
}
}
结果字符串。添加(a);
for(字符串r:结果字符串){
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++;    
    ...
}
希望这有帮助

试试简单的代码

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++;    
    ...
}
希望这有帮助

试试简单的代码

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++;    
    ...
}
希望这有帮助

试试简单的代码

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++;    
    ...
}
希望这对你也有帮助

for(int i = 0; i < array.length; i++) {

    if(i = (array.length - 1)) {

        //this is the last element in the array
    }
}
for(int i=0;i
您也可以这样做

for(int i = 0; i < array.length; i++) {

    if(i = (array.length - 1)) {

        //this is the last element in the array
    }
}
for(int i=0;i
您也可以这样做

for(int i = 0; i < array.length; i++) {

    if(i = (array.length - 1)) {

        //this is the last element in the array
    }
}
for(int i=0;i
您也可以这样做

for(int i = 0; i < array.length; i++) {

    if(i = (array.length - 1)) {

        //this is the last element in the array
    }
}
for(int i=0;i
如果您只需要抓取集合的最后一个元素并向其添加一些文本,那么这应该可以工作

String out = "Some value";    
int lastIndex = pending.getSize() -1; // < 0 indexed
String lastItem = pending.get(lastIndex)
String newLastItem = lastItem + out; // < String concatenation
String out=“一些值”;
int lastIndex=pending.getSize()-1;//<0索引
字符串lastItem=pending.get(lastIndex)
字符串newLastItem=lastItem+out;//<字符串串联
但是从你的片段中,我不认为这是你想要的,因为如果我们去掉一些神奇的数字,修正缩进,对你想要做的做一些假设

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

        a += pn;
    } else {
        // have string longer than max length, so save and start a new sms.
        resultingStrings.add(a);
        Log.d("messages", a); // < log a before you overwrite it.
        a = pn;
    }
}

// DO YOU WANT TO APPEND out AS A SUFFIX TO a HERE ???
// a += out;
// but if so youll want to consider the case if a is now > MAX_LENGTH

resultingStrings.add(a); // add the last string

// send all composed strings
for (String r : resultingStrings) {
    sendSMS(r);
}
字符串a=out+“-”+rptdate+“-”;
int prefixLength=a.长度();
用于(字符串pn:挂起){
//检查总值+挂起长度值是否小于160
如果(a.length()+pn.length()前置桥长度){
a+=“,”;
}
a+=pn;
}否则{
//字符串长度超过最大长度,请保存并启动新的sms。
结果字符串。添加(a);
Log.d(“消息”,a);//<在覆盖它之前先记录a。
a=pn;
}
}
//是否要在此处作为后缀追加???
//a+=输出;
//但是如果是这样的话,如果A现在是Max长度,你会考虑这个情况。
结果字符串。添加(a);//添加最后一个字符串
//发送所有合成字符串
for(字符串r:结果字符串){
sendSMS(r);
}

我选择了相对较新的代码,所以我建议您首先从一些伪代码开始,它可以成为代码中的注释。始终保持代码格式良好,以便缩进匹配,并为变量和常量使用描述性名称

如果您只需要抓取集合的最后一个元素并向其添加一些文本,那么这应该可以工作

String out = "Some value";    
int lastIndex = pending.getSize() -1; // < 0 indexed
String lastItem = pending.get(lastIndex)
String newLastItem = lastItem + out; // < String concatenation
String out=“一些值”;
int lastIndex=pending.getSize()-1;//<0索引
字符串lastItem=pending.get(lastIndex)
字符串newLastItem=lastItem+out;//<字符串串联
但是从你的片段中,我不认为这是你想要的,因为如果我们去掉一些神奇的数字,修正缩进,对你想要做的做一些假设

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

        a += pn;
    } else {
        // have string longer than max length, so save and start a new sms.
        resultingStrings.add(a);
        Log.d("messages", a); // < log a before you overwrite it.
        a = pn;
    }
}

// DO YOU WANT TO APPEND out AS A SUFFIX TO a HERE ???
// a += out;
// but if so youll want to consider the case if a is now > MAX_LENGTH

resultingStrings.add(a); // add the last string

// send all composed strings
for (String r : resultingStrings) {
    sendSMS(r);
}
字符串a=out+“-”+rptdate+“-”;
int prefixLength=a.长度();
用于(字符串pn:挂起){
//检查总值+挂起长度值是否小于160
如果(a.length()+pn.length()前置桥长度){
a+=“,”;
}
a+=pn;
}否则{
//字符串长度超过最大长度,请保存并启动新的sms。
结果字符串。添加(a);
Log.d(“消息”,a);//<在覆盖它之前先记录a。
a=pn;
}
}
//是否要在此处作为后缀追加???
//a+=输出;
//但是如果是这样的话,如果A现在是Max长度,你会考虑这个情况。
结果字符串。添加(a);//添加最后一个字符串
//发送所有合成字符串
for(字符串r:结果字符串){
sendSMS(r);
}

我选择了相对较新的代码,所以我建议您首先从一些伪代码开始,它可以成为代码中的注释。始终保持代码格式良好,以便缩进匹配,并为变量和常量使用描述性名称

如果您只需要抓取集合的最后一个元素并向其添加一些文本,那么这应该可以工作

String out = "Some value";    
int lastIndex = pending.getSize() -1; // < 0 indexed
String lastItem = pending.get(lastIndex)
String newLastItem = lastItem + out; // < String concatenation
String out=“一些值”;
int lastIndex=pending.getSize()-1;//<0索引
字符串lastItem=pending.get(lastIndex)
字符串newLastItem=lastItem+out;//<字符串串联
但是从你的片段中,我不认为这是你想要的,因为如果我们去掉一些神奇的数字,修正缩进,对你想要做的做一些假设

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

        a += pn;
    } else {
        // have string longer than max length, so save and start a new sms.
        resultingStrings.add(a);
        Log.d("messages", a); // < log a before you overwrite it.
        a = pn;
    }
}

// DO YOU WANT TO APPEND out AS A SUFFIX TO a HERE ???
// a += out;
// but if so youll want to consider the case if a is now > MAX_LENGTH

resultingStrings.add(a); // add the last string

// send all composed strings
for (String r : resultingStrings) {
    sendSMS(r);
}
字符串a=out+“-”+rptdate+“-”;
int prefixLength=a.长度();
用于(字符串pn:挂起){
//检查总值+挂起长度值是否小于160
如果(a.length()+pn.length()前置桥长度){
a+=“,”;
}
a+=pn;
}否则{
//字符串长度超过最大长度,请保存并启动新的sms。