Avoid explicit array creation for vararg parameters

This commit is contained in:
Dmitry Cherniachenko
2017-04-18 22:16:52 +02:00
committed by Egor.Ushakov
parent d149b53799
commit dfd90978c9
6 changed files with 193 additions and 6 deletions

View File

@@ -0,0 +1,29 @@
package pkg;
public class TestVarArgCalls {
public void doSmth() {
printAll("Test");
printAll("Test: %s", "abc");
printAll("Test: %s - %s", "abc", "DEF");
printComplex("Test");
printComplex("Test: %[0]s", new String[] { "abc" });
printComplex("Test: %[0]s - %[0]s", new String[] { "abc" }, new String[] { "DEF" });
String.format("Test");
String.format("Test: %d", 123);
String.format("Test: %d - %s", 123, "DEF");
Object[] data = { "Hello" };
String.format("Test: %s", (Object) data);
String.format("Test: %s", (Object[]) data);
}
public void printAll(String fmt, String... params) {
System.out.println(String.format(fmt, (Object[]) params));
}
public void printComplex(String fmt, String[]... params) {
System.out.println(String.format(fmt, (Object[]) params));
}
}