Multiple Java Variable-Length Arguments

I have not seen a special thing until today when working with a variable length argument

For example, there is a method called ready statement with a declaration in which

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. Declaring another method

  String prepareStatement(int i,String... columnNames,String... values) //still the same result as above(The variable ...... parameter) 

Why doesn't Java allow multiple variable length arguments? Is there any other way to achieve this?

PS: The reason for this is my requirement - to generate a generalized prepared statement for the passed parameter, since all this parameter will be passed through the properties

+6
source share
5 answers

Only the last parameter is allowed for the Length variable:

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

String ... is equal to String [], so in this case you can insert String [] for the first parameter and just check if it is empty or how long it will be.

Edit Your Editing

If you really need an input for all your lines as parameters, I would recommend defining a really very unusual line to separate 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 also specify the length of the column name as a parameter:

 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 a call:

 System.out.println(prepareStatement(5, "a","b","c","d","e","f","g","h","i","j","k")); 

And the same conclusion:

 abcde values fghijk 
+18
source

Why doesn't Java allow multiple variable length arguments? Is there any other way to achieve this?

I believe in this expression:

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

If you call a method like

 prepareStatement("x","y"); 

It is not possible to allow "x" and "y" to belong to that argument.

+6
source

This requires only one varlength argument, because if there are more, they may not be able to distinguish between them.

Look at the first example with some input:

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

and name it with:

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

Although it might be logical to split the arguments evenly, which cannot be accepted by the compiler, since it does not know your intentions to split it in the middle. In order to save itself from mess like inferring and sticky edge cases, this does not allow this even for different types.

+4
source

As the JVM understands, when String... columnNames ends and runs String... values if you use it like this:

 prepareStatement("one", "two", "three"); 
+3
source

If you are not very good at using strings as parameters, it will be easier for you to take a HashMap as a parameter. There is no need for analysis and can take any number of parameters.

0
source

Source: https://habr.com/ru/post/950533/


All Articles