Java – what is the meaning of the three points in the function parameters?

There is already an answer to this question: > java, 3 dots in parameters8

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

The question is, what are the three points of doinbackground (URL... URLs)?

Solution

This is not an Android feature This is a Java feature (added in Java 5), so you can use the "custom" parameter

This method:

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

And this:

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

The internal method is the same The whole difference is the way you call For the first (also known as varargs), you can do this simply:

doInBackground(url1,url2,url3,url4);

But for the second, you have to create an array, so if you try to do this on one line, it's like:

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

Well, if you try to call a method written in varargs in this way, it will work in the same way as whether or not (backward support)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>