I am struggling with this basic code below ,
how do i prevent the last comma "," from being appended to the String.
String outScopeActiveRegionCode="";
List<String> activePersons=new ArrayList<String>();
HashSet<String> outScopeActiveRegionCodeSet=new HashSet<String>();
for (String person : activePersons) {
outScopeActiveRegionCodeSet.add(person);
}
Iterator itr = outScopeActiveRegionCodeSet.iterator();
while(itr.hasNext()){
outScopeActiveRegionCode+=itr.next();
outScopeActiveRegionCode+=",";
}
Id actually do it the other way around, id append the comma before on all cases except the first, its easier.
boolean isFirst = true;
while(itr.hasNext()) {
if(isFirst) {
isFirst = false;
} else {
outScopeActiveRegionCode+=",";
}
outScopeActiveRegionCode+=itr.next();
}
The reason for this is that it is much simpler to detect the first case than the last case.
I would do:
String delimiter = "";
while(itr.hasNext()){
outScopeActiveRegionCode += delimiter;
outScopeActiveRegionCode += itr.next();
delimiter = ",";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.