Java的多个可变长度参数 [英] Java multiple variable length argument
问题描述
我在可变长度参数工作时,还没有看到今天之前的特别的事情
有关,例如,有ppared语句声明了一个名为方法$ P $,这样
1
字符串prepareStatement(字符串... columnNames,字符串...值)
//字符串... columnNames(Eclipse中显示错误,说的方法prepareStatement变量参数类型字符串必须是最后一个参数)
2 另一种方法声明
字符串prepareStatement(INT I,串... columnNames,字符串...值)
//仍然相同的结果与上述(可变参数......)
为什么Java的不允许多个可变长度参数?有没有达到那么其他的方式?
PS:之所以这样做是我的要求是产生过去了,因为这一切的参数将通过属性来传递的参数广义prepared声明
只有最后一个参数允许为变长:
字符串prepareStatement(的String [] columnNames,字符串...值)
串......等于为String []所以在这种情况下,你可以插入一个String []的第一个参数,只是检查其空或者它有多长。
修改您的修改
如果你真的需要一个输入的所有字符串作为参数,我会建议定义一个真的很罕见的字符串要分开你输入:
静态字符串prepareStatement(字符串... PARAMS)
{
字符串RET =;
布尔valueInput = FALSE;
对于(字符串S:PARAMS)
{
如果(s.equals(MyReallyUncommonSeperateString))
{
valueInput =真;
RET + =\ nvalues \ N的; columnNames和价值观//视觉分隔符
}
否则,如果(valueInput)
{
//处理你的价值投入
RET + = S; //例如处理,拼接一切
}
其他
{
//处理您columnnames的
RET + = S; //例如处理,拼接一切
}
}
返回RET;
}
您可以把它叫做:
<$p$p><$c$c>System.out.println($p$ppareStatement("a","b","c","d","e","MyReallyUncommonSeperateString","f","g","h","i","j","k"));
输出:
ABCDE
值
fghijk
另一种方法是,得到columnNames作为参数的长度以及
静态字符串prepareStatement(INT长度字符串... PARAMS)
{
字符串RET =;
的for(int i = 0; I&LT;长度;我++){
//处理columnnames的
字符串COLNAME = PARAMS [I]
//做些事情COLNAME
RET + = COLNAME; //例如处理,拼接一切
}
RET + =\ nvalues \ N的;对columnNames ANS值//视觉分隔符
的for(int i =长度; I&LT; params.length;我++){
字符串值= PARAMS [I]
//做一些与价值观
RET + =价值; //例如处理,拼接一切
}
返回RET;
}
在呼叫:
的System.out.println(prepareStatement(5,A,B,C,D,E,F,克,H,I,J,K));
和相同的输出:
ABCDE
值
fghijk
I have not seen the particular thing before today when working on variable length argument
For e.g., There is a method named prepared statement with declaration such that
1.
String prepareStatement(String... columnNames,String... values)
//String... columnNames(Eclipse shows error saying The variable argument type String of the method prepareStatement must be the last parameter)
2. Another method declaration
String prepareStatement(int i,String... columnNames,String... values)
//still the same result as above(The variable ...... parameter)
Why does java not allow multiple variable length argument?? Is there other way to achieve so?
P.S: The reason for doing so is my requirement is to generate generalized prepared statement for the parameter passed, since all this parameter will be passed via properties
Only the last Parameter is allowed to be variable Length:
String prepareStatement(String[] columnNames, String... values)
String... is equal to String[] so in this case you could insert a String[] for the first parameter and just check if its empty or how long it is.
Edit to your Edit
If you really need an input for all your Strings as Parameters I would recommend to define a really really uncommon String to seperate your inputs:
static String prepareStatement(String... params)
{
String ret = "";
boolean valueInput = false;
for(String s : params)
{
if(s.equals("MyReallyUncommonSeperateString"))
{
valueInput = true;
ret+="\nvalues\n";//visual delimiter of columnNames and Values
}
else if(valueInput)
{
//handling of your value inputs
ret+=s; //example handling, concatenate everything
}
else
{
//handling of your columnnames
ret+=s; //example handling, concatenate everything
}
}
return ret;
}
You can call it:
System.out.println(prepareStatement("a","b","c","d","e","MyReallyUncommonSeperateString","f","g","h","i","j","k"));
Output:
abcde
values
fghijk
Another way is to give the length of the columnNames as parameter as well:
static String prepareStatement(int length, String... params)
{
String ret = "";
for(int i = 0; i < length; i++){
//handling of columnnames
String colName = params[i];
//do something with colName
ret+=colName; //example handling, concatenate everything
}
ret+="\nvalues\n";//visual delimiter of columnNames ans Values
for(int i = length; i < params.length; i++){
String value = params[i];
//do something with values
ret+=value; //example handling, concatenate everything
}
return ret;
}
With the call:
System.out.println(prepareStatement(5, "a","b","c","d","e","f","g","h","i","j","k"));
And the same output:
abcde
values
fghijk
这篇关于Java的多个可变长度参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!