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[]
.
(不传递任何参数的toArray()
方法将返回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()]
.
(重要更新 :最初,以上代码使用new String[list.size()]
。)
However, this blogpost reveals that due to JVM optimizations, using new String[0]
is better now. (但是, 此博文显示由于JVM优化,现在使用new String[0]
更好。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…