9

what does the "..." in each function mean? and why in the last function, there is no "..."?

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");
     }
 }
zoey
  • 529
  • 1
  • 6
  • 14
  • 2
    Read Here : http://stackoverflow.com/questions/3158730/java-3-dots-in-parameters – DPP Mar 11 '13 at 01:20

4 Answers4

13

As Morrison said, the ... syntax is for a variable length list of arguments (urls holds more than one URL).

This is typically used to allow users of the AsyncTask to do things like (in your case) pass in more than one URL to be fetched in the background. If you only have one URL, you would use your DownloadFilesTask like this:

DownloadFilesTask worker = new DownloadFilesTask();
worker.execute(new URL("http://google.com"));   

or with multiple URLs, do this:

worker.execute(new URL[]{ new URL("http://google.com"), 
                          new URL("http://stackoverflow.com") });

The onProgressUpdate() is used to let the background task communicate progress to the UI. Since the background task might involve multiple jobs (one for each URL parameter), it may make sense to publish separate progress values (e.g. 0 to 100% complete) for each task. You don't have to. Your background task could certainly choose to calculate a total progress value, and pass that single value to onProgressUpdate().

The onPostExecute() method is a little different. It processes a single result, from the set of operations that were done in doInBackground(). For example, if you download multiple URLs, then you might return a failure code if any of them failed. The input parameter to onPostExecute() will be whatever value you return from doInBackground(). That's why, in this case, they are both Long values.

If doInBackground() returns totalSize, then that value will be passed on onPostExecute(), where it can be used to inform the user what happened, or whatever other post-processing you like.

If you really need to communicate multiple results as a result of your background task, you can certainly change the Long generic parameter to something other than a Long (e.g. some kind of collection).

Nate
  • 31,017
  • 13
  • 83
  • 207
4

In Java its called Varargs which allow for a variable number of parameters.

http://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html

Morrison Chang
  • 11,691
  • 3
  • 41
  • 77
2

The three dots are ... are used to indicate ellipsis, In our case in Java Language these are used to indicate varangs (variable no. of arguments).

Let me explain a little bit about varangs:

The varangs allows the method to accept zero or muliple arguments.If we don't know how many argument we will have to pass in the method, varargs is the better approach.

Syntax of varargs:

The varargs uses ellipsis i.e. three dots after the data type. Syntax is as follows:

return_type method_name(data_type... variableName){}

Simple Example of Varargs in java:

class VarargsExample1{  

 static void display(String... values){  
  System.out.println("display method invoked ");  
 }  

 public static void main(String args[]){  

 display();//zero argument   
 display("my","name","is","varargs");//four arguments  
 }   
}  

Rules for varargs:

While using the varargs, you must follow some rules otherwise program code won't compile. The rules are as follows:

There can be only one variable argument in the method. Variable argument (varargs) must be the last argument.

Prashanth
  • 993
  • 8
  • 18
1

Very short (and basic) answer: That represents a variable number of items "converted" to an array and it should be the last argument. Example:

test("string", false, 20, 75, 31);

void test(String string, boolean bool, int... integers) {
    // string = "string"
    // bool = false
    // integers[0] = 20
    // integers[1] = 75
    // integers[2] = 31
}

But you can also call

test("text", true, 15);

or

test("wow", true, 1, 2, 3, 4, 5, 6, 7, 8, 9, 100, 123, 345, 9123);

4face
  • 590
  • 1
  • 8
  • 18