Pregunta

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

¿Fue útil?

Solución

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

Otros consejos

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top