1

I am working on a Java class that contains this field:

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?

Tnx

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • possible duplicate of [Convert ArrayList to String \[\]](http://stackoverflow.com/questions/5374311/convert-arrayliststring-to-string) – home Nov 25 '14 at 14:44

3 Answers3

2

You might notice if you have a quick look at the Collecion javadoc that collections are supposed to implement a toArray method.

So yes, you can at anytime initialize an Array variable using the array returned by the toArray method of your collection. (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).

You would thus write something like :

YourType[] sal= yourCollection.toArray(new YourType[0]);
Loïc Gammaitoni
  • 4,173
  • 16
  • 39
0

If you have a List then you can use T[] toArray(T[] a)

A4L
  • 17,353
  • 6
  • 49
  • 70
0

Would this work for you?

sal = myCollection.toArray(new com.enel.xmlns.EDILM.SalReport.SalDettaglio[myCollection.size()])
Predrag Maric
  • 23,938
  • 5
  • 52
  • 68