ArrayList to String Array Conversion in Java

There are essentially three ways to convert ArrayList that contains String objects to convert in to String Array. Since ArrayList can contain any object, even though String object type is used in this example, this is equally valid for other data types as well.

First two methods are covered just for the completion. For practical purposes, you should be able to use just the third method.

Method 1:

This is a very basic method, it is essentially about creating an Array of the type String and the size of the list, then loop through the list and add one element at a time.

Method 2:

toArray() method will return Object Array. From the Object Array you can use Arrays.copyOf method to copy in to a String array

Method 3:

Use toArray() method with String Array as argument.

Below is the sample code for all the methods

Display:

 

Click Here to Leave a Comment Below

Leave a Reply: