Skip to content

Instantly share code, notes, and snippets.

@hofiorg
Last active May 11, 2016 11:05
Show Gist options
  • Save hofiorg/be6e9447321d1157a5ae17e7e12725cf to your computer and use it in GitHub Desktop.
Save hofiorg/be6e9447321d1157a5ae17e7e12725cf to your computer and use it in GitHub Desktop.
Java - Variable Arguments
public class Varargs2 {
public static String insertComma(String... values) {
String result = "";
boolean first = true;
for (String value : values) {
result += (first ? "" : ", ") + value;
first = false;
}
return result;
}
public static void main(String[] args) {
System.out.println(insertComma("hallo"));
System.out.println(insertComma("hallo", "welt"));
System.out.println(insertComma("hallo", "welt", "123"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment