10

I was reading about AsyncTask in android documentation.

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
             // Escape early if cancel() is called
             if (isCancelled()) break;
         }
         return totalSize;
     }

     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
 }

Question is doInBackground(URL... urls) what are these 3 dots?

laalto
  • 150,114
  • 66
  • 286
  • 303
wordpressm
  • 3,189
  • 3
  • 20
  • 29

5 Answers5

9

This is not a Android feature. This is an Java feature (added in Java 5) that was included so you can have "custom" arguments.

This method:

protected Long doInBackground(URL... urls) {
  for (URL url : urls) {
    // Do something with url
  }
}

and this one:

protected Long doInBackground(URL[] urls) {
  for (URL url : urls) {
    // Do something with url
  }
}

for the internal method are the same. The whole difference is on the way you invoke that. For the first one (also known as varargs) you can simply do this:

doInBackground(url1,url2,url3,url4);

But for the second on you have to create a array, so if you try to do this in just one line it will be like:

doInBackground(new URL[] { url1, url2, url3 });

The good thing is that if you try to call the method that was written using varargs on that way it will work at the same way that if it wasn't (backward support).

Caleryn
  • 1,084
  • 3
  • 18
  • 23
endrigoantonini
  • 1,191
  • 2
  • 15
  • 28
4

It means this function takes variable number of arguments - its arity is variable. For example, all those are valid invocations (assuming this is bound to AsyncTask's instance):

this.doInBackground();                 // Yes, you could pass no argument
                                       // and it'd be still valid.
this.doInBackground(url1);
this.doInBackground(url1, url2);
this.doInBackground(url1, url2, url3);
// ...

Inside your method body, urls will be basically an array. One interesting thing about varargs is that you could invoke such method in two ways - either by passing an array of arguments, or by specifing each url as a separate method argument. So, those two are equivalent:

this.doInBackground(url1, url2, url3);
this.doInBackground(new URL[] {url1, url2, url3});

You can use for loop to go through all of them:

protected Long doInBackground(URL... urls) {
  for (URL url : urls) {
    // Do something with url
  }
}

You can of course define similar method on your own, for instance:

public void addPerson (String name, String... friends) {
  // ...
}

In this example, your method would accept one mandatory argument (name, you cannot ommit this one) and variable number of friends arguments:

this.addPerson();               // INVALID, name not given
this.addPerson("John");         // Valid, but no friends given.
this.addPerson("John," "Kate"); // Valid, name is John and one friend - Kate

Not that this construct is available since Java 5. You can find documentation here.

kamituel
  • 34,606
  • 6
  • 81
  • 98
3

These three dots denote the variable-length arguments to the function.

Sergo Pasoevi
  • 2,833
  • 3
  • 27
  • 41
2

This question shows little research effort, since there are multiple duplicates already on Stackoverflow:

What do three dots (...) indicate when used as a part of parameters during method definition?

What is it called when you use object... as a parameter?

What are the 3 dots in parameters?/What is a variable arity (...) parameter?

Community
  • 1
  • 1
bosnjak
  • 8,424
  • 2
  • 21
  • 47
1

Those three dots there means variable number of arguments can be passed to the function which we access using progress[0] or progress[1] and so on.

Yogesh D
  • 1,663
  • 2
  • 23
  • 38