I have a method
public boolean findANDsetText (String Description, String ... extra ) {
inside I want to call another method and pass it extras
but I want to add new element (Description) to extras.
object_for_text = getObject(find_arguments,extra);
How can I do that in java? What would the code look like?
I tired to accommodate code from this question but couldn't make it work.
Every time we use varargs, the Java compiler creates an array to hold the given parameters. In this case, the compiler creates an array with generic type components to hold the arguments. The varargs usage is safe if and only if: We don't store anything in the implicitly created array.
Important Points regarding Varargs Before JDK 5, variable length arguments could be handled in two ways: One was using overloading, other was using array argument. There can be only one variable argument in a method. Variable argument (Varargs) must be the last argument.
Use Arrays.copyOf(...)
:
String[] extra2 = Arrays.copyOf(extra, extra.length+1);
extra2[extra.length] = description;
object_for_text = getObject(find_arguments,extra2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With