java Java泛型中的3个点是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17623217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 02:29:49  来源:igfitidea点击:

What do the 3 dots in Java generics mean?

java

提问by maysi

for example I have a code like this: (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) {
      }
}

and what do the 3 dots in the parameter of the method do?

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

回答by William Morrison

The three dots are referred to as varargsand here, allow you to pass more than one string to the method like so:

这三个点被称为varargs和 在这里,允许您将多个字符串传递给方法,如下所示:

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

Documentation on that here.

此处的文档。

Within the method doInBackgroundyou can enumerate over the varargs variable, paramslike so:

在该方法中,doInBackground您可以枚举 varargs 变量,params如下所示:

for(int i=0;i<params.length;i++){
    System.out.println(params[i]);
}

So its basically an array of strings within the scope of doInBackground

所以它基本上是一个范围内的字符串数组 doInBackground

回答by chancea

The compiler treats the three dots ...as taking in an array of that object. In this case Stringand Void. The amount of objects you pass in is the size of the array.

编译器将三个点...视为接收该对象的数组。在这种情况下StringVoid。您传入的对象数量是数组的大小。

Thus:

因此:

doInBackground("Hi", "Hello", "Bye")will create an array of Stringof length 3.

doInBackground("Hi", "Hello", "Bye")将创建一个String长度为 3的数组。

回答by Michael Lang

This concept is called varargsand explained here

这个概念称为varargs在此处解释