[英]Can I initialize a Java array with another collection?
I am working on a Java class that contains this field: 我正在研究包含以下字段的Java类:
private com.enel.xmlns.EDILM.SalReport.SalDettaglio[] sal;
this is an array. 这是一个数组。 Is it possibile initizialize this sal object with some other collection (a list) type in some way?
难道不可能性initizialize与其他一些集合(列表)型在某种程度上这SAL对象?
Tnx TNX
You might notice if you have a quick look at the Collecion javadoc that collections are supposed to implement a toArray method. 您可能会注意到,如果您对Collecion javadoc有所了解,那么集合应该实现toArray方法。
So yes, you can at anytime initialize an Array variable using the array returned by the toArray method of your collection. 所以是的,您可以随时使用集合的toArray方法返回的数组初始化Array变量。 (but you'll have to pay attention that the generic type of your collection correpond to the type of your array variable, and might have to cast what the toArray methods return to fit the type of your variable).
(但您必须注意,集合的通用类型与数组变量的类型相对应,并且可能必须强制转换toArray方法返回的内容以适合您的变量类型)。
You would thus write something like : 因此,您将编写如下内容:
YourType[] sal= yourCollection.toArray(new YourType[0]);
如果有List
则可以使用T [] toArray(T [] a)
这对您有用吗?
sal = myCollection.toArray(new com.enel.xmlns.EDILM.SalReport.SalDettaglio[myCollection.size()])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.