Java 多变长参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17924999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 19:37:43  来源:igfitidea点击:

Java multiple variable length argument

javaandroidmethodsvariadic-functionsmethod-declaration

提问by Akhil Jain

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

例如,有一个名为prepared statement的方法,其声明使得

1.

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

2.另一种方法声明

  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?

为什么java不允许多个变长参数??还有其他方法可以实现吗?

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

PS:这样做的原因是我的要求是为传递的参数生成通用的准备语句,因为所有这些参数都将通过属性传递

回答by Loki

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.

String... 等于 String[] 所以在这种情况下你可以为第一个参数插入一个 String[] 并检查它是否为空或它有多长。

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:

另一种方法是将 columnNames 的长度也作为参数:

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

回答by NINCOMPOOP

Why does java not allow multiple variable length argument?? Is there other way to achieve so?

为什么java不允许多个变长参数??还有其他方法可以实现吗?

I believe with this declaration :

我相信这个声明:

String prepareStatement(String... columnNames,String... values) 

If you invoke the method as

如果您调用该方法

prepareStatement("x","y");

There is no way to resolve "x"and "y"belong to which argument.

没有办法解决"x""y"属于哪个论点。

回答by nanofarad

It requires there to only be one varlength argument because if there are more it may not be able to tell them apart.

它要求只有一个 varlength 参数,因为如果有更多参数,它可能无法区分它们。

Let's look at the first example with some input:

让我们看看第一个带有一些输入的示例:

String prepareStatement(String... columnNames,String... values)

and call it with:

并调用它:

String prepareStatement("foo", "bar", "baz", "foov", "barv", "bazv");

While perhaps it would be logical for the arguments to be split evenly, that can't be assumed by the compiler since it knows not your intentions of having it be split down the middle. In order to save itself from a mess of type inferring and sticky edge cases, it doesn't allow this even for different types.

虽然将参数平均分割可能是合乎逻辑的,但编译器无法假设,因为它不知道您将其从中间分割的意图。为了避免混乱的类型推断和粘性边缘情况,即使对于不同的类型,它也不允许这样做。

回答by nkukhar

How would JVM understand when String... columnNamesends and starts String... valuesif you use it like this:

如果您像这样使用它,JVM 将如何理解何时String... columnNames结束和开始String... values

prepareStatement("one", "two", "three");

回答by gvmani

If you are not very particular about using string as the parameters, will be easier to take HashMap as a parameter. No need of parsing and can take any number of parameters as well.

如果不是很讲究用string做参数,拿HashMap做参数会更方便。不需要解析,也可以接受任意数量的参数。