How do I convert collection into array? - JAVA Interview Questions

How do I convert collection into array?


To convert collection-based data into array we can use toArray() method provided by the implementation of Collection interface such as java.util.ArrayList.


import java.util.List;
import java.util.ArrayList;

public class CollectionToArrayExample
{
public static void main(String[] args)
{
List list = new ArrayList();
list.add("Java");
list.add("Sample");
list.add("Code");

Object[] array = list.toArray();
for (int i = 0; i < style="color: rgb(0, 102, 0);">length; i++)
{
System.out.println(array[i].toString());
}
}
}

Our sample code result is shown below:


Java
Sample
Code


Click Here to See Answer .....
Did you like this article ?
Subscribe to my RSS feed and get more JAVA Question, and Guideline, Plus a lot more great advice to help your Software Career.

0 comments:

Related JAVA Questions Posts