List to Array Java

List to Array Java

Here, toArray() method of Collections interface is used. This method returns an Object array containing the elements of collection class.

Example on on "List to Array Java" explains the usage of toArray() method to convert List to array.


List to Array Java
Output screen on List to Array Java

A List object cityList is created and then added with three elements with add() method.

The List cityList object is converted to an object array with toArray() method defined in Collection interface (inherited by List interface), the super most interface of all DS of Java. Using enhanced for loop all the elements of object array cityObjectArray.

All the elements of array cityStringArray are printed at a time (avoiding a loop iteration) with toString() method of class Arrays.

Java comes with many classes grouped as categories. Would you like to know?

Leave a Reply

Your email address will not be published. Required fields are marked *