'Why can't we use array initialization in variable arguments? [duplicate]
As far as I know, variable arguments form their own array type with the received arguments, is there a limit to the use of expressions that initialize the array? Or is there a memory that I don't know or other side effects?
import java.util.StringJoiner;
public class VarArgsEx {
public static void main(String[] args) {
String[] strArr = { "100", "200", "300" };
System.out.println(concatenate("", "100", "200", "300"));
System.out.println(concatenate("-", strArr));
System.out.println(concatenate(",", new String[]{"1", "2", "3"}));
System.out.println("["+concatenate(",", new String[0]) + "]");
System.out.println("["+concatenate(",")+"]");
System.out.println(concatenate("+", {"100", "200", "300"}));
}
static String concatenate(String delim, String... args) {
StringJoiner sj = new StringJoiner(delim);
for (String arg: args)
sj.add(arg);
return sj.toString();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
