什么时候更喜欢varargs列表到数组? [英] When to prefer a varargs list to an array?

查看:100
本文介绍了什么时候更喜欢varargs列表到数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一个API,你可以通过一个方法来传递程序从中读取资源的路径列表

  public void importFrom(String ... paths){

}

I使用varargs使方法尽可能方便地调用用户,如此

  obj.importFrom(/ foo ,/ foo / bar); 

这是否适合使用varargs?或者传入一个数组更好?

解决方案

在你的情况下,varargs就好了。你真的不需要创建一个路径数组您将要导入,因为除了将它们传递给 importFrom 方法之外,您不想对这些路径做任何事情。



varargs功能使您不必显式创建一个数组,仅仅是为了将一组值传递给一次性方法,您似乎在这里。



BTW,如果你想



<$,你可以仍然传入数组p $ p> public class VarargsDemo {
public static void f(String ... args){
for(String s:args){
System.out。的println(一个或多个);
}
}
public static void main(String [] args){
String [] english = new String [] {one,two,three };
f(英文);
f(uno,dos,tres);
}
}

因为行为相同,差异就会降低关于你希望方法签名说的一个(可能是次要的)问题。当你声明一个方法来获取一个显式的数组参数时,它几乎就像你想要强调你想要对一个数组对象进行操作,这个方法在方法之外定义并且在方法之外有自己的存在和重要性,并且也许像索引这样的操作很重要。当用varargs声明方法时,就像你说只给我一堆物品。



然后,这不一定是真的; JVM不知道它的区别,它看到的只是一个运行时的数组。许多程序员不会因为方法签名的意图而分裂头发。 Varargs就是为了方便拨打电话。



那就是说,varargs的主要限制是这样的参数必须是最后一个方法。在你的情况下,这不是问题,但通常需要考虑。


I'm implementing an API an have a method which you pass a list of paths where the program reads resources from

public void importFrom(String... paths) {

}

I'm using varargs to make calling the method as convenient as possible to the user, like so

obj.importFrom("/foo", "/foo/bar);

Is this an appropriate use of varargs? Or is passing in an array better?

解决方案

In your case varargs is just fine. You don't really need to make an array of the paths that you will be importing because there's nothing you want to do with these paths other than to pass them along to your importFrom method.

The varargs functionality saves you from having to explicitly create an array solely for the purpose of passing a collection of values to a one-off method, which you do appear to have here.

BTW, you can still pass in an array if you want to

public class VarargsDemo {
    public static void f(String... args) {
        for (String s: args) {
            System.out.println(s);
        }
    }
    public static void main(String[] args) {
        String[] english = new String[]{"one", "two", "three"};
        f(english);
        f("uno", "dos", "tres");
    }
}

Because the behavior is the same, the difference comes down to a (probably minor) question of what you want the method signature to "say". When you declare a method to take an explicit array parameter, it's almost as if you want to stress that you want to operate on an array object, something that has been defined outside the method and has its own existence and importance outside the method, and one in which, perhaps, operations like indexing matter. When declaring the method with varargs, its as if you are saying "just give me a bunch of items".

Then again, this doesn't have to be true; the JVM doesn't know the difference, all it sees is an array at run time. Many programmers won't bother splitting hairs over the intent of the method signature. Varargs is all about making the calls convenient.

That said, the main limitation of varargs is that such a parameter must be the last one of the method. In your case this is not a problem, but in general it is something to consider.

这篇关于什么时候更喜欢varargs列表到数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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