一系列字符串与字符串Varargs [英] An array of Strings vs String Varargs

查看:115
本文介绍了一系列字符串与字符串Varargs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

void方法(String [] a) void方法(String ... a)

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

The first method take an array of Strings where as the second method takes one or more String arguments. What are the different features that they provide?

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

Moreover, don't know why but this is working:

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 
    }
}


推荐答案

没有区别,只有在签名后面还有其他元素。

There is no difference, only if there are other elements after it in the signature.

例如:

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

是可能的,因为jvm无法认为 user 仍然是 args的元素

is possible, since there is no way the jvm will think user is still an element of args.

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

会造成麻烦。

这篇关于一系列字符串与字符串Varargs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆