转换'ArrayList<String>到 Java 中的 字符串 []"

本教程将介绍转换'ArrayList<String>到 Java 中的“字符串 []"的处理方法,这篇教程是从别的地方看到的,然后加了一些国外程序员的疑问与解答,希望能对你有所帮助,好了,下面开始学习吧。

转换'ArrayList<String>到 Java 中的 字符串 []" 教程 第1张

问题描述

How might I convert an ArrayList<String> object to a String[] array in Java?

解决方案

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.

好了关于转换'ArrayList&lt;String&gt;到 Java 中的“字符串 []"的教程就到这里就结束了,希望趣模板源码网找到的这篇技术文章能帮助到大家,更多技术教程可以在站内搜索。