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?
Three dots mean that there method can get as parameters as much argument of type Object as it likes. Reading more about "varargs" arguments could be helpful. In short, it's a syntactic sugar for array with restriction that this should be the last parameter in arguments list.
Technically it is ellipsis, but more commonly called “…”, dots, dot-dot-dot or three-dots. Both of these allow you to give them as many arguments as you like, and you can name those arguments (which end up as names in the resulting object). function. function (X, MARGIN, FUN, ...) > args (apply) function (X, MARGIN, FUN, ...) NULL
The three-dots makes such abbreviation problematic. The rule is that if the argument is after the three-dots in the function definition, then it can not be abbreviated. Such is the case with recursive recursive in c c.
There is a mechanism that allows variability in the arguments given to R functions. Technically it is ellipsis, but more commonly called “…”, dots, dot-dot-dot or three-dots. Both of these allow you to give them as many arguments as you like, and you can name those arguments (which end up as names in the resulting object).
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).
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.
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