Skip to content

Instantly share code, notes, and snippets.

@LarryBattle
Created March 21, 2013 22:31
Show Gist options
  • Save LarryBattle/5217402 to your computer and use it in GitHub Desktop.
Save LarryBattle/5217402 to your computer and use it in GitHub Desktop.
Join ArrayList in java. Combines all the elements into a string separated the `delimiter`.
/**
* Combines all the elements into a string seperated the `delimiter`.
* Same as `Array.prototype.join()` in Javascript.
* @example
ArrayList<String> x = new ArrayList<String>();
x.add("1");
x.add("2");
joinArrayListString(x, ",").equals("1,2") == true;
*/
public String joinArrayListString(ArrayList<String> r, String delimiter) {
if(r == null || r.size() == 0 ){
return "";
}
StringBuffer sb = new StringBuffer();
int i, len = r.size() - 1;
for (i = 0; i < len; i++){
sb.append(r.get(i) + delimiter);
}
return sb.toString() + r.get(i);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment