top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to convert a list to array in Java?

+1 vote
292 views
How to convert a list to array in Java?
posted Apr 14, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Converted a list to an array can be accomplished by calling the toArray() method of the list with a parameter indicating the type of array to create. The ListToArrayTest class takes a list of strings and gets an array of strings from this list.

ListToArrayTest.java    
package test;    
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;    
import org.apache.commons.lang.ArrayUtils;

public class ListToArrayTest {    
    public static void main(String[] args) throws IOException {    
        List<String> strList = new ArrayList<String>();
        strList.add("string1");
        strList.add("string2");
        strList.add("string3");

        String[] strArray = (String[]) strList.toArray(new String[0]);
        System.out.println(ArrayUtils.toString(strArray));
    }
}

Here is the output from ListToArrayTest.

Results

{string1,string2,string3}
answer Apr 15, 2016 by Karthick.c
...