java中的“字符串...”是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9718314/
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-08-16 06:39:02  来源:igfitidea点击:

What is "String..." in java?

javaandroidstring

提问by AnothrWu

Possible Duplicate:
varargs and the '…' argument
Java, 3 dots in parameters

可能的重复:
varargs 和 '...' 参数
Java,参数中的 3 个点

I saw this definition in my android java file. It looks just like String[]. Are they different? Thank you.

我在我的 android java 文件中看到了这个定义。它看起来就像 String[]。它们不同吗?谢谢你。

回答by Willem Van Onsem

It's syntax for writing the items of the array as a parameter

将数组的项作为参数写入的语法

for instance:

例如:

 public String first (String... values) {
     return values[0];
 }

Then you can call this method with first("4","2","5","67")

然后你可以调用这个方法 first("4","2","5","67")

The javacompiler will create an array of the parameters on its own.

javacompiler 将自己创建一个参数数组。

回答by Nishant

varags. If a method signature is method(Param param, String... x)it will take one Paramtype of object and anynumber of String objects.

varags. 如果方法签名是method(Param param, String... x),它将采用一种Param类型的对象和任意数量的 String 对象。

There are couple if cool things about it:

有一些很酷的事情:

  1. It's nothing special. It's just sugared array. So, method(MyObject... o)is same as method(MyObject[] o).

  2. Vararg has to bethe last parameter in argument list.

  3. There is this funny thing that bit me once. method(MyObject... o)can be called as method()without any compilation error. Java will internally convert the no-arg call to method(new MyObject[0]). So, be aware of this.

  1. 这没什么特别的。这只是加糖的数组。所以,method(MyObject... o)和 一样method(MyObject[] o)

  2. Vararg必须是参数列表中的最后一个参数。

  3. 有一件有趣的事情曾经咬过我。method(MyObject... o)可以调用 asmethod()没有任何编译错误。Java 会在内部将无参数调用转换为method(new MyObject[0]). 所以,请注意这一点。

回答by alexg

It's for defining a method with a variable number of arguments.

它用于定义具有可变数量参数的方法

回答by user1252434

It's a vararg, variable number of arguments. In the method body you treat it as a String[], but when you call the method you can either chose to supply a String[] or simply enumerate your values.

这是一个可变参数,可变数量的参数。在方法主体中,您将其视为 String[],但是当您调用该方法时,您可以选择提供 String[] 或简单地枚举您的值。

void foo(String... blah) { }

void bar() {
   String[] a = { "hello", "world" };
   foo(a);  // call with String[]
   foo("hello", "world"); // or simply enumerate items
}

Was introduced with Java 5.

是在 Java 5 中引入的。

回答by AlexR

Stringis a string type. String[]is an array of strings.

String是字符串类型。 String[]是一个字符串数组。

String ...is a syntactic sugar named ellipsis, introduced in java 1.5 and taken from C. It can be used in methods definitions and actually the same as array with only one difference. If method is defined as:

String ...是一个名为 ellipsis 的语法糖,在 java 1.5 中引入并取自 C。它可以用于方法定义中,实际上与数组相同,只有一个区别。如果方法定义为:

public void foo(String[] arg){}

public void foo(String[] arg){}

you must pass array to it:

您必须将数组传递给它:

foo(new String[] {"a", "b"});

foo(new String[] {"a", "b"});

If method is defined as:

如果方法定义为:

public void foo(String arg){}

public void foo(String arg){}

You can call it either

你也可以叫它

foo(new String[] {"a", "b"});

foo(new String[] {"a", "b"});

or

或者

foo("a", "b");

foo("a", "b");