This question already has answers here:
What is the ellipsis (…) for in this method signature?

(5个答案)


7年前关闭。




例如,我有这样的代码:(from here)
private class LongOperation extends AsyncTask<String, Void, String> {

      @Override
      protected String doInBackground(String... params) {}

      @Override
      protected void onPostExecute(String result) {}

      @Override
      protected void onPreExecute() {}

      @Override
      protected void onProgressUpdate(Void... values) {
      }
}

该方法的参数中的三个点是做什么的?

最佳答案

这三个点称为varargs,在这里,您可以像这样将多个字符串传递给该方法:

doInBackground("hello","world");
//you can also do this:
doInBackground(new String[]{"hello","world"});

Documentation on that here.

doInBackground方法中,您可以枚举varargs变量params,如下所示:
for(int i=0;i<params.length;i++){
    System.out.println(params[i]);
}

因此,它基本上是doInBackground范围内的字符串数组

关于java - Java泛型中的三个点是什么意思? [复制],我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17623217/

10-11 02:06