Java,将字符串数组作为方法参数传递的最有效方法[duplicate]

时间:2023-01-19 00:17:29

This question already has an answer here:

这个问题已经有了答案:

I have the following code

我有以下代码

String[] args = {"a", "b", "c"};
method(args);


private void method(String[] args){
    return args;
}

Why can I not do the following without errors?

为什么我不能做到以下没有错误?

method({"a", "b", "c"});

This code is example just to prove the point, not the actual methods I am using. I would like to do the second method instead to clean up my code, and avoid declaring a dozen different arrays when I only use them once to pass to my method.

这段代码只是为了证明这一点,而不是我正在使用的实际方法。我想用第二种方法来清理我的代码,并且当我只使用一次来传递给我的方法时,避免声明一打不同的数组。

The heart of the question is what is the most efficient way to pass an array of strings as a method paramter.

问题的核心是,作为方法参数传递字符串数组的最有效方式是什么。

4 个解决方案

#1


6  

I suspect you want to use varargs. You don't even need to create an array to sent variable length arguments.

我猜你是想用varargs。甚至不需要创建一个数组来发送变量长度参数。

String[] strings = method("a", "b", "c");

private String[] method(String... args){
    return args;
}

or

String[] strings = array("a", "b", "c");

private <T> T[] array(T... args){
    return args;
}

or if you want to condense futher

或者如果你想进一步压缩

String[] strings = array("a, b, c");

private String[] array(String args){
    return args.split(", ?");
}

#2


16  

try

试一试

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

that way the system knows it is a new string-array.

这样系统就知道它是一个新的字符串数组。

java is not like php ;)

java不像php;)

#3


6  

If you use:

如果你使用:

method({ "a", "b", "c"});

then java has no idea whether you want an array of String or of Object. You can explicitly tell java what kind of array it is like this:

那么java不知道是要字符串数组还是对象数组。你可以明确地告诉java它是什么样的数组:

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

That way Java can tell that you mean an array of String.

这样Java就可以知道你指的是一个字符串数组。

#4


2  

You do not need a named reference to the array. You can initialize and pass an anonymous array like this:

不需要对数组进行命名引用。您可以初始化并传递一个匿名数组,如下所示:

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

#1


6  

I suspect you want to use varargs. You don't even need to create an array to sent variable length arguments.

我猜你是想用varargs。甚至不需要创建一个数组来发送变量长度参数。

String[] strings = method("a", "b", "c");

private String[] method(String... args){
    return args;
}

or

String[] strings = array("a", "b", "c");

private <T> T[] array(T... args){
    return args;
}

or if you want to condense futher

或者如果你想进一步压缩

String[] strings = array("a, b, c");

private String[] array(String args){
    return args.split(", ?");
}

#2


16  

try

试一试

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

that way the system knows it is a new string-array.

这样系统就知道它是一个新的字符串数组。

java is not like php ;)

java不像php;)

#3


6  

If you use:

如果你使用:

method({ "a", "b", "c"});

then java has no idea whether you want an array of String or of Object. You can explicitly tell java what kind of array it is like this:

那么java不知道是要字符串数组还是对象数组。你可以明确地告诉java它是什么样的数组:

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

That way Java can tell that you mean an array of String.

这样Java就可以知道你指的是一个字符串数组。

#4


2  

You do not need a named reference to the array. You can initialize and pass an anonymous array like this:

不需要对数组进行命名引用。您可以初始化并传递一个匿名数组,如下所示:

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