简体   繁体   中英

Incompatible types when converting List to String array

I seem to be getting an error when trying to convert a list to an array, this is my code

List text = loadFile(pathName); String[] convertedData = text.toArray(new String[text.size()]);

The error I'm getting is:

incompatible types: java.lang.Object[] cannot be converted to java.lang.String[].

Not really sure why and if anymore information is needed ill be happy to post, any help would be greatly appreciated.

Use a generic List of String s:

List<String> text = loadFile(pathName);

and make sure that the loadFile method returns a List<String> .

The problem is that you're calling the parameterless overload of toArray() , which returns Object[] . You can't assign an Object[] to an String[] variable. To fix the problem do this:

 List<String> text = loadFile(pathName);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM