How might I convert an ArrayList<String> object to a String[] array in Java?
How might I convert an ArrayList<String> object to a String[] array in Java?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
For example:
The
toArray()method without passing any argument returnsObject[]. So you have to pass an array as an argument, which will be filled with the data from the list, and returned. You can pass an empty array as well, but you can also pass an array with the desired size.Important update: Originally the code above used
new String[list.size()]. However, this blogpost reveals that due to JVM optimizations, usingnew String[0]is better now.