In python, I can do:
args = [1,2,3,4]
f(*args) # this calls f(1,2,3,4)
Is this possible in java?
to clarify – f has an argument list of variable length.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Sure, you should be able to do precisely that using vararg-methods. If you’re worried about ambiguities when it comes to arguments such as
Object...this piece of code should clarify:Output:
You can not do it for a non-vararg method. However, a non-vararg method has a fixed number of arguments, so you should be able to do
Further more, there is no way to let the compiler resolve the situation for overloaded methods based on the size or type of the array.