Casting ArrayList <String> to String []
1) I wonder why I can not do this:
ArrayList<String> entries = new ArrayList<String>();
entries.add("entry");
String[] myentries = (String[])entries.toArray();
What is wrong with that? (You can ignore the second line of code, this does not apply to the question)
2) I know that my goal can be achieved using this code:
ArrayList<String> entries = new ArrayList<String>();
entries.add("entry");
String[] myentries = new String[entries.size()];
myentries = entries.toArray(myentries)
Is this the preferred way to convert an ArrayList to a String Array? Is there a better / shorter way?
Many thanks: -)
+5
4 answers