Java Example – ArrayList to String Array

Java Tutorial for Beginners
Java Tutorial for Beginners
Java Tutorial for Beginners
Java Tutorial for Beginners

In this Java example we will see how to convert ArrayList to String Array . The example below shows how to convert ArrayList to String array in Java.

 * Created by
import java.util.ArrayList;
import java.util.Arrays;

public class ArrayListToStringArray {

    public static void main(String args[]){

        //ArrayList containing string objects
        ArrayList<String> nameList = new ArrayList<String>();

                 * To convert ArrayList containing String elements to String array, use
                 * Object[] toArray() method of ArrayList class.
                 * Please note that toArray method returns Object array, not String array.

        //First Step: convert ArrayList to an Object array.
        Object[] objNames = nameList.toArray();

        //Second Step: convert Object array to String array

        String[] strNames = Arrays.copyOf(objNames, objNames.length, String[].class);

        System.out.println("ArrayList converted to String array");

        //print elements of String array
        for(int i=0; i < strNames.length; i++){

ArrayList converted to String array


Partner Sites

Be the first to comment

Leave a Reply

Your email address will not be published.