What does the three dots in "doInBackground (URL ... urls)" mean?

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"); } } 
+7
source share
4 answers

As Morrison said , the syntax ... is for a variable-length argument list ( urls contains more than one URL ).

This is usually used to allow AsyncTask users to do things such as (in your case) pass more than one URL that will be selected in the background. If you have only one URL, you should use your DownloadFilesTask as follows:

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

or with multiple urls, do the following:

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

onProgressUpdate() used to cause the background task to report progress to the user interface. Since the background task may include multiple tasks (one for each URL parameter), it may be advisable to publish separate progress values ​​(for example, from 0 to 100%) for each task. You don’t have to. Your background task, of course, could calculate the total progress value and pass that single value to onProgressUpdate() .

The onPostExecute() method is slightly different. It processes the result of single , from a set of operations performed in doInBackground() . For example, if you download multiple URLs, you can return a failure code if any of them failed. The input parameter to onPostExecute() will be any value that you return from doInBackground() . Therefore, in this case both values ​​are Long .

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

If you really need to convey several results as a result of a background task, you can, of course, change the general Long parameter to something other than Long (for example, some kind of collection).

+12
source

In Java, it is called Varargs, which allow a variable number of parameters.

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

+4
source

Three dots ... are used to denote ellipsis . In our case, in Java, they are used to denote varangs (variable number of arguments).

Let me explain varangs a bit :

varangs allows the method to accept null or to mull arguments. If we don’t know how many arguments we will have to pass the method, varargs is the best approach.

Syntax varargs:

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

return_type method_name(data_type... variableName){}

A 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:

When using varargs, you must follow some rules, otherwise the program code will not compile. The rules are as follows:

There can only be one variable argument in a method. variable Argument (varargs) should be the last argument.

+1
source

A very short (and basic) answer: This represents a variable number of elements "converted" to an array, and this 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);

0
source

All Articles