在Java中将String的ArrayList转换为String数组
首先,让我们设置字符串的ArrayList-
ArrayList<String> arrList = new ArrayList<String>(); arrList.add("Bentley"); arrList.add("Audi"); arrList.add("Jaguar"); arrList.add("Cadillac");
现在,使用toArray()转换为字符串数组-
int size = arrList.size(); String res[] = arrList.toArray(new String[size]);
示例
以下是在Java中将String的ArrayList转换为String数组的程序-
import java.util.*; public class Demo { public static void main(String[] args) { ArrayList<String> arrList = new ArrayList<String>(); arrList.add("Bentley"); arrList.add("Audi"); arrList.add("Jaguar"); arrList.add("Cadillac"); arrList.add("Mazda"); arrList.add("Land Rover"); arrList.add("Porsche"); int size = arrList.size(); String res[] = arrList.toArray(new String[size]); System.out.println("String Array..."); for(String s: res) { System.out.println(s); } } }
输出结果
String Array... Bentley Audi Jaguar Cadillac Mazda Land Rover Porsche