By using toArray() method we can convert a Collection into an Array.
toArray() : This method is provided as a bridge between Collections and older APIs that expect Arrays on input. The Array operations allow the contents of a Collection to be translated into an Array.
- The simple form with no arguments creates a new Array of Object. The more complex form allows the caller to provide an Array or to choose the runtime type of the output Array.
Examples :
- choose c is a Collection. The following snippet dumps the contents of c into a newly allocated Array of Object whose length is identical to the number of elements in c.
Object[] a = c.toArray();
- If c is known to contain only Strings, the following snippet dumps the contents of c into a newly allocated Array of String whose length is identical to the number of elements in c.
String[] a = c.toArray(new String[0]);
toArray() : This method is provided as a bridge between Collections and older APIs that expect Arrays on input. The Array operations allow the contents of a Collection to be translated into an Array.
- The simple form with no arguments creates a new Array of Object. The more complex form allows the caller to provide an Array or to choose the runtime type of the output Array.
Examples :
- choose c is a Collection. The following snippet dumps the contents of c into a newly allocated Array of Object whose length is identical to the number of elements in c.
Object[] a = c.toArray();
- If c is known to contain only Strings, the following snippet dumps the contents of c into a newly allocated Array of String whose length is identical to the number of elements in c.
String[] a = c.toArray(new String[0]);
No comments:
Post a Comment