How might I convert an ArrayList<String>
object to a String[]
array in Java?
转载于:https://stackoverflow.com/questions/4042434/converting-arrayliststring-to-string-in-java
How might I convert an ArrayList<String>
object to a String[]
array in Java?
转载于:https://stackoverflow.com/questions/4042434/converting-arrayliststring-to-string-in-java
收起
当前问题酬金
¥ 0 (可追加 ¥500)
支付方式
扫码支付
List<String> list = ..;
String[] array = list.toArray(new String[0]);
For example:
List<String> list = new ArrayList<String>();
//add some stuff
list.add("android");
list.add("apple");
String[] stringArray = list.toArray(new String[0]);
The toArray()
method without passing any argument returns Object[]
. 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, using new String[0]
is better now.
报告相同问题?