Java multiple variable length argument

馋奶兔 提交于 2019-12-01 17:24:17

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

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

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.

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

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.

How would JVM understand when String... columnNames ends and starts String... values if you use it like this:

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!