为什么我不能内联创建新的Java数组? [重复]

时间:2021-05-24 20:42:57

This question already has an answer here:

这个问题在这里已有答案:

Why does the first one work and the second not work?

为什么第一个工作而第二个工作不起作用?

1) OK

1)好的

String[] foo = {"foo"};
bar.setArray(foo);

2) BAD

2)坏

bar.setArray({"foo"});

Is there a quick way to create a String[] on a single line?

有没有快速的方法在一行上创建一个String []?

4 个解决方案

#1


33  

bar.setArray(new String[] { "foo" });

I believe this format is required because Java does not want to imply the array type. With the array initialization format, the type is defined explicitly by the assigned variable's type. Inline, the array type cannot be inferred.

我相信这种格式是必需的,因为Java不想暗示数组类型。使用数组初始化格式,类型由指定的变量的类型显式定义。内联,无法推断数组类型。

#2


6  

As others have said:

正如其他人所说:

bar.setArray(new String[] {"foo"});

It was planned to allow getting rid of the new String[] in J2SE 5.0, but instead we have varargs. With varargs, you can slightly change the the declaration of setArray to use ... in place of [], and ditch the new String[] { }.

计划允许在J2SE 5.0中删除新的String [],但我们有varargs。使用varargs,您可以稍微更改setArray的声明以使用...代替[],并抛弃新的String [] {}。

public final class Bar {
    public void setArray(String... array) {
    [...]
}

[...]
    bar.setArray("foo"); 

#3


2  

You should use this:

你应该用这个:

bar.setArray(new String[] {"foo"});

bar.setArray(new String [] {“foo”});

#4


0  

Unfortunately, the closest that Java comes to inline arrays is new String[]{"foo", "bar"} however there is a neat trick that allows you to do something like

不幸的是,Java最接近内联数组的是新的String [] {“foo”,“bar”}但是有一个巧妙的技巧可以让你做类似的事情

array("foo", "bar") with the type automatically inferred.

自动推断出类型的数组(“foo”,“bar”)。

I have been working on a useful API for augmenting the Java language to allow for inline arrays and collection types. For more details google project Espresso4J or check it out here

我一直在研究一种有用的API,用于扩充Java语言以允许内联数组和集合类型。有关详细信息,请参阅Google Project Espresso4J或在此处查看

#1


33  

bar.setArray(new String[] { "foo" });

I believe this format is required because Java does not want to imply the array type. With the array initialization format, the type is defined explicitly by the assigned variable's type. Inline, the array type cannot be inferred.

我相信这种格式是必需的,因为Java不想暗示数组类型。使用数组初始化格式,类型由指定的变量的类型显式定义。内联,无法推断数组类型。

#2


6  

As others have said:

正如其他人所说:

bar.setArray(new String[] {"foo"});

It was planned to allow getting rid of the new String[] in J2SE 5.0, but instead we have varargs. With varargs, you can slightly change the the declaration of setArray to use ... in place of [], and ditch the new String[] { }.

计划允许在J2SE 5.0中删除新的String [],但我们有varargs。使用varargs,您可以稍微更改setArray的声明以使用...代替[],并抛弃新的String [] {}。

public final class Bar {
    public void setArray(String... array) {
    [...]
}

[...]
    bar.setArray("foo"); 

#3


2  

You should use this:

你应该用这个:

bar.setArray(new String[] {"foo"});

bar.setArray(new String [] {“foo”});

#4


0  

Unfortunately, the closest that Java comes to inline arrays is new String[]{"foo", "bar"} however there is a neat trick that allows you to do something like

不幸的是,Java最接近内联数组的是新的String [] {“foo”,“bar”}但是有一个巧妙的技巧可以让你做类似的事情

array("foo", "bar") with the type automatically inferred.

自动推断出类型的数组(“foo”,“bar”)。

I have been working on a useful API for augmenting the Java language to allow for inline arrays and collection types. For more details google project Espresso4J or check it out here

我一直在研究一种有用的API,用于扩充Java语言以允许内联数组和集合类型。有关详细信息,请参阅Google Project Espresso4J或在此处查看