This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Now this makes sense. Your ArrayList contains Strings. Then, when using the toArray method, you try to put all those elements into an Integer. Of course Integer cannot store String, that's where the ArrayStoreException comes from. As specified in the Javadoc of java.util.Collection:
Change Integer to String and it will work.
Mind you, I do think this is a mistake in the API. The method signature is "<T> T toArray(T a)" - it should have been "<T super E> T toArray(T a)" to enforce that T is E or a superclass of E (or an interface that is implemented by E). In your example, with E being String, that would limit T to one of the following: - String - Object - Serializable - CharSequence - Comparable<String> [ July 26, 2008: Message edited by: Rob Prime ]