javaandroidmethodsvariadic-functionsmethod-declaration

Java multiple variable length argument


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


Solution

  • 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