How might I convert an ArrayList<String>
object to a String[]
array in Java?
Java – Converting ‘ArrayList to ‘String[]’ in Java
arraylistarrayscollectionsjavastring
Related Topic
- Java – Create ArrayList from array
- Java – How to read / convert an InputStream into a String in Java
- Java – When to use LinkedList over ArrayList in Java
- Python – Convert bytes to a string
- Java – Initialization of an ArrayList in one line
- Javascript – How to replace all occurrences of a string in JavaScript
- Java – How to convert a String to an int in Java
- Java – Why is processing a sorted array faster than processing an unsorted array
Best Answer
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.