Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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");
     }
 }
like image 779
zoey Avatar asked Sep 30 '12 22:09

zoey


1 Answers

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);

like image 177
4face Avatar answered Nov 15 '22 20:11

4face