0

I am currently trying to get AsyncTasks working in Android, and have found something I have never seen before over and over again in many different tutorials.

Certain methods in the tutorials are passed parameters that look like this String... arg0, Integer... values.

Here is a tutorial with some code shown similar to what I am describing.

What does this mean? Why is the ... there?

JuiCe
  • 4,132
  • 16
  • 68
  • 119

1 Answers1

2

It is called varargs. It works for any type as long as it's the last argument in the signature.

Basically, any number of parameters are put into an array. This does not mean that it is equivalent to an array.

A method that looks like:

void foo(int bar, Socket baz...)

will have an array of Socket (in this example) called baz.

So, if we call foo(32, sSock.accept(), new Socket()) we'll find an array with two Socket objects.

Calling it as foo(32, mySocketArray) will not work. However, if the signature is a varargs of arrays you can pass one or more arrays and get a two-dimensional array. For example, void bar(int bar, PrintStream[] baz...) can take multiple arrays of PrintStream and stick them into a single PrintStream[][].

nanofarad
  • 40,330
  • 4
  • 86
  • 117