Given a method such as:
public void blit(int[] pos) {
do stuff..
}
Is there a way to pass in a list of arguments without first declaring them?
For instance, in Python, I could do something like:
def blit(pos):
stuf...
blit([20,50])
I can simply pass in a list that created specifically in the argument list. To do the equivalent in Java, it seems I have to first declare the array, and only then can I pass it to the method.
int[] coordinates = {20,50};
blit(coordinates);
Which is not a huge deal, but it’d be handy if I could just do something like,
blit({20,50});
But that, of course, doesn’t work. I’ve also tried creating the array inside of the call like,
blit(new int[] = {20,40});
But again, no luck. So, perhaps a terrible question, but is there a way to pass lists of arguments to a method that mimics in part the behavior of Python?
You could do it in two ways:
Or declare your method with varargs:
and call it like this: