English 中文(简体)
Java varargs 方法清单
原标题:Java varargs method param list vs. array

Varargs:

public static void foo(String... string_array) { ... }

versus

单一阵列段:

public static void bar(String[] string_array) { ... }

Java 1.6似乎接受/反对如下:

String[] arr = {"abc", "def", "ghi"};
foo(arr);  // accept
bar(arr);  // accept
foo("abc", "def", "ghi");  // accept
bar("abc", "def", "ghi");  // reject

假设上述情况是真实的/正确性的,为什么并不总是使用弹性而不是单一阵列。 Seems to add aplica of callser flexiblity for free.

Can an expert share the internal JVM difference, if there is one?

感谢。

最佳回答

从贾瓦伊一开始,阿雷拉就近了,而瓦拉尔是最近增加的。 因此,许多旧法典仍然使用阵列。

还指出,采用具有明确阵列参数的通用加固法,可能会造成与预期不同的行为:

public <T> void foo(T... params) { ... }

int[] arr = {1, 2, 3};

foo(arr); // passes an int[][] array containing a single int[] element

因此,除了要求作出大量努力以争取不明显的好处外,并不总是用蒸气取代遗留阵列参数。

更不用提一下你可以开车的情况,因为计算参数清单中阵列之后还有另一个参数:

public void foo(String[] strings, String anotherParam) { ... }

调整参数在技术上可以解决这一问题,但会打破客户代码。

<>Update: 有效 Java 2nd。 英文版,项目42:Use varargs judiciously,用更详细的方式解释这一点,并举一个具体的例子:Arrays.asList(在Java5中被改装,使其具有变数参数,inadvertently 打破了许多现有的代码,在使用(现为陈旧的)部件时可能会引起惊讶:

System.out.println(Arrays.asList(myArray));

<><>Update2: 双重核对了来文方,指出问题产生者有各种原始类型,如<代码>int[。 在加固之前,照此办理:

int[] digits = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 4 };
System.out.println(Arrays.asList(digits));

将产生一个汇编错误,因为只有各种参考类型才能转换成<代码>List 。 由于蒸汽和改装asList,上述代码汇编无警告,意外结果如<代码>[[I@3e25a5]”。

问题回答

不把所有东西都具体化的主要原因是,它并不总是有意义。 例如,如果InputStream.read(byte [] 在被定义为“现(......)”的情况下,以下呼吁是有效的:

myInputStream.read(0, 1, 2, 3);

This would create a 4-element byte array, pass it in and then discard it.

蒸汽是一阵种简单的合成糖。

if you call foo("abc", "def", "ghi"); then compiler will call it as foo(new String[] {"abc", "def", "ghi"});

compiler will create one new array and pass it to foo(). One can t have both foo(String...) and foo(String[]). Since both are functionally same.

in foo you specify three params, you would have to call bar like this:

 bar(new String[]{"abc", "def", "ghi"});

so that you only call it with one parameter, that is the String[] in this case this has alsmost nothing to do with internals, your method signature for the method bar simply states that it only has one param, whereas foo has n params which are all strings

This is, how varargs are defined. The varargs extension do not make every array accepting function a varargs function. You have to call bar like this:

bar(new String[]{"abc", "def", "ghi"})

另一个差异是效率。 在一阵列内的物体被援引。 然而,当方法推向四舍五入时,对变式理由清单参数进行了评估。

This is apparent when a function call is passed as a parameter that returns the type that is used in the variable argument list.

Example: someMethod( Object... x) anotherMethod( Object [] );

某些标准(a)、(b)、(c)、(a)、(b)和c)将在你采用这种方法之前援引。

另一边 方法(新目标) 页: 1 在接触这些物体之前使用这些方法。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签