3

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.

1
  • 1
    If loadFile(pathName) returns a list of String write List<String> text = loadFile(pathName); Commented Jan 16, 2016 at 17:30

2 Answers 2

2

Use a generic List of Strings:

List<String> text = loadFile(pathName);

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

Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.