Any way to declare an array in

Let's say I have a method m() that takes an array of Strings as an argument. Is there a way I can just declare this array in-line when I make the call? ie Instead of:

String[] strs = {"blah", "hey", "yo"};
m(strs);

Can I just replace this with one line, and avoid declaring a named variable that I'm never going to use?


m(new String[]{"blah", "hey", "yo"});

Draemon is correct. You can also declare m as taking varargs:

void m(String... strs) {
    // strs is seen as a normal String[] inside the method
}

m("blah", "hey", "yo"); // no [] or {} needed; each string is a separate arg here

另一种方法可以做到这一点,如果你想把结果作为List inline,你可以这样做:

Arrays.asList(new String[] { "String1", "string2" });
链接地址: http://www.djcxy.com/p/28340.html

上一篇: 内存中的位置是我的变量存储在C中?

下一篇: 任何方式来声明一个数组