How to convert Collection to Array in Java?

Ads

 
 

Share on Google+Share on Google+

Java Coder
How to convert Collection to Array in Java?
2 Answer(s)      6 months and 7 days ago
Posted in : Java Beginners

Hello,

I have a collection object in data with data in it. How to convert Collection to Array in Java?

Thanks

Ads
View Answers

April 13, 2017 at 6:35 AM


Hi,

You can use the following code to create a collection:

  List<String> myList = new ArrayList<String>();
  list.add("This ");

Above code create a collection and then add an item to it.

Now to convert it into list myList.toArray() function can be used which converts Collection to Array in Java.

Here is the code for converting Collection to Array:

 String[] s1 = myList.toArray(new String[0]);

Check complete tutorial at Converting Collection to an Array.

Thanks



April 13, 2017 at 6:36 AM


Hi,

Check 100s of Java Util Examples.

Thanks


Ads









Ads

 
Advertisement null

Ads