0

I have this code:

java.util.List<String> result = new java.util.LinkedList<>();

And have added some strings to it.

But I want to return a String[]. Ideally I'd like to write

return (String[])result.toArray();

But I get a casting error at runtime. (Cannot convert Object[] to String[]). Is there a way round this that doesn't involve manual element by element copy?

1
  • 1
    Use the overloaded method toArray(T[] arr). Commented Apr 24, 2014 at 13:41

2 Answers 2

5

Try this:

return result.toArray(new String[result.size()]);
Sign up to request clarification or add additional context in comments.

Comments

0

Exactly for this there is the toArray(T[] arr) method so you can pass it a String array

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.