gpt4 book ai didi

java - 字符串数组与字符串可变参数

转载 作者:行者123 更新时间:2023-12-01 07:03:17 26 4
gpt4 key购买 nike

void method(String[] a)void method(String... a) 有什么区别?

第一个方法采用字符串数组,而第二个方法采用一个或多个字符串参数。它们提供哪些不同的功能?

此外,不知道为什么,但这是有效的:

public class Test {

public static void main(String[] args) {
String[] a = {"Hello", "World"};
Test t = new Test();
t.method(a);
}

void method(String...args) {
System.out.println("Varargs"); // prints Varargs
}
}

最佳答案

没有区别,只是签名中后面有其他元素。

例如:

public void method(String[] args, String user){}

是可能的,因为 jvm 不可能认为 user 仍然是 args 的元素。

public void method(String ... args, String user){}

不过会引起麻烦。

关于java - 字符串数组与字符串可变参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35455230/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com