Q:

How to cast an ArrayList to array of strings

A problem I had:
If you want to export an ArrayList of strings into an array of strings you might try:
ArrayList<String> lines = new ArrayList<String>();
...
lines.add(line); // etc
...
return (String[]) lines.toArray();

and get the error:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
    at wormbase.model.parser.FileParser.getDataObj(FileParser.java:45)
    at wormbase.model.parser.ModelParser.main(ModelParser.java:18)

referencing this, toArray() will return a list of objects not strings.
return lines.toArray(new String[lines.size()]);

fixes the issue.
Edit: Thanks gangnamstyleoverflow

A:

try 
lines.toArray(new String[lines.size()])

no args toArray returns an Object[] check here , thus ClassCastException.
 whereas  overloaded toArray(T) returns an T[]. 

