Mouad EL Fakir Mouad EL Fakir - 2 months ago 8
Java Question

java List of Strings to Array of Strings

I want to know if it's possible to convert a Java

List
of
String
s to an array of
String
s:

I tried this:

List<String> products = new ArrayList<String>();
//some codes..
String[] arrayCategories = (String[]) products.toArray();


but it gives me an exception message:


java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.lang.String[]

Answer
String[] array = products.toArray(new String[products.size()]);