Java 字符串...关于方法的多个输入的问题
Java String... question regarding multiple inputs for a method
是否可以创建一个 class 并拥有一个 String ... 属性,它可以接受与您输入的字符串一样多或一样少的字符串?
例子:
请原谅我粗糙的伪代码,这是 java.
//this is the method:
public void getXXXX(String ...) {
//random code executes in a loop with as many as strings that are inputted
}
//this code calls it
getXXXX("Benjamin","Jordan","Steve")
getXXXX("Pengu","No")
getXXXX("hi")
是的,你输入的内容或多或少会起作用,你只需要在你的类型后面有一个参数名称。
class StringDecorator {
public static String join(final String... strings) {
final var builder = new StringBuilder();
for (final var string : strings) {
builder.append(string);
}
return builder.toString();
}
}
然后在某处调用它
StringDecorator.join("Hello, ", "World!"); // "Hello, World!"
是否可以创建一个 class 并拥有一个 String ... 属性,它可以接受与您输入的字符串一样多或一样少的字符串? 例子: 请原谅我粗糙的伪代码,这是 java.
//this is the method:
public void getXXXX(String ...) {
//random code executes in a loop with as many as strings that are inputted
}
//this code calls it
getXXXX("Benjamin","Jordan","Steve")
getXXXX("Pengu","No")
getXXXX("hi")
是的,你输入的内容或多或少会起作用,你只需要在你的类型后面有一个参数名称。
class StringDecorator {
public static String join(final String... strings) {
final var builder = new StringBuilder();
for (final var string : strings) {
builder.append(string);
}
return builder.toString();
}
}
然后在某处调用它
StringDecorator.join("Hello, ", "World!"); // "Hello, World!"