Java 中的 ArrayList.toArray() 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9874121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ArrayList.toArray() method in Java
提问by java_mouse
I am wondering why did they design the toArray
method in ArrayList
to take a input of an array in Java?
我想知道为什么他们设计了在 Java 中输入数组的toArray
方法ArrayList
?
ArrayList<String> listArray = new ArrayList<String>();
listArray.add("Germany");
listArray.add("Holland");
listArray.add("Sweden");
String []strArray = new String[3];
String[] a = (String [])listArray.toArray(strArray);
To me it appears that, they dont need this input because the instance of the ArrayList itself has enough details to convert the data into an array.
在我看来,他们不需要这个输入,因为 ArrayList 的实例本身有足够的细节来将数据转换为数组。
My question is why do they still need the array to be passed in? Thanks.
我的问题是为什么他们仍然需要传入数组?谢谢。
回答by Andrzej Doyle
Two reasons I can think of:
我能想到的两个原因:
- Erasure means that the generic parameters aren't available at runtime, so an
ArrayList<String>
doesn't knowthat it contains strings, it's just the raw typeArrayList
. Thus all invocations oftoArray()
would have to return anObject[]
, which isn't strictly correct. You'd have to actually create a second array ofString[]
then iterate over the first, casting all of its parameters in turn to come out with the desired result type. - The way the method is defined means that you can pass in a reference to an existing array, and have this populated via the method. In some cases this is likely very convenient, rather than having a new array returned and then copying its values elsewhere.
- 擦除意味着泛型参数在运行时不可用,因此
ArrayList<String>
不知道它包含字符串,它只是原始类型ArrayList
。因此,所有对 的调用toArray()
都必须返回一个Object[]
,这并不完全正确。您必须实际创建第二个数组,String[]
然后迭代第一个数组,依次转换其所有参数以得到所需的结果类型。 - 定义方法的方式意味着您可以传入对现有数组的引用,并通过该方法填充它。在某些情况下,这可能非常方便,而不是返回一个新数组然后将其值复制到其他地方。
回答by Mathias Schwarz
In your code, the ArrayList
can contain anything, not only Strings. You could rewrite the code to:
在您的代码中,ArrayList
可以包含任何内容,而不仅仅是字符串。您可以将代码重写为:
ArrayList<String> listArray = new ArrayList<String>();
listArray.add("Germany");
listArray.add("Holland");
listArray.add("Sweden");
String []strArray = new String[3];
String[] a = listArray.toArray(strArray);
However, in Java arrays contain their content type (String) at runtime, while generics are erased by the compiler, so there is still no way for the runtime system to know that it should create a String[]
in your code.
但是,在 Java 中,数组在运行时包含它们的内容类型(String),而泛型会被编译器擦除,因此运行时系统仍然无法知道它应该String[]
在您的代码中创建一个。
回答by Louis Wasserman
You need it to get array types other than Object[]
. listArray.toArray(new String[3])
shouldn't actually need a cast to String[]
, but it lets you write into arrays for specific types, rather than the (rather unsafe) approach of just casting Object[]
to whatever your type is.
您需要它来获取除Object[]
. listArray.toArray(new String[3])
实际上不应该需要转换为String[]
,但它允许您写入特定类型的数组,而不是(相当不安全的)方法只是转换Object[]
为您的类型。
回答by Chandra Sekhar
It is not compulsory to pass Array as an argument to toArray(), you can directly call arrayList.toArray()
不强制将 Array 作为参数传递给 toArray(),可以直接调用arrayList.toArray()
As we know that Collection framework with generics works with the concept of Type Erasure. So the arrayList becomes list of Object, so when you convert to array it will give an Object[]if you dont pass any argument.
正如我们所知,带有泛型的 Collection 框架与Type Erasure的概念一起工作。所以 arrayList 变成了对象列表,所以当你转换为数组时,如果你不传递任何参数,它会给出一个Object[]。
回答by Sanjay T. Sharma
ArrayList
doesn't have enough information given that the "data" itself is stored in buckets of Object
arrays. So, if you know "what" kind of items you have in your array list, you can achieve type safety by using this variant of the toArray
method.
ArrayList
鉴于“数据”本身存储在Object
数组桶中,没有足够的信息。因此,如果您知道数组列表中有“什么”类型的项目,则可以通过使用该toArray
方法的这种变体来实现类型安全。
回答by Churk
It is so that when you have an non-empty array you want object to be appended to from the arraylist conversion you can do that without having to create a new array, and do post processing of merging it, save resource and extra work.
因此,当您有一个非空数组时,您希望从数组列表转换中附加对象,您可以这样做而无需创建新数组,并进行合并后的处理,节省资源和额外的工作。
Read the second paragraph of the javadoc: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/ArrayList.html#toArray(T[])
阅读 javadoc 的第二段:http://docs.oracle.com/javase/1.5.0/docs/api/java/util/ArrayList.html#toArray(T[])
回答by Ashok Raj
Because the array into which the elements are to be copied should be specified to the JVM.
因为要将元素复制到的数组应指定给 JVM。