提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
ArrayList到数组的转换
import java.util.ArrayList; import java.util.Arrays; /** * n o w j a v a . c o m 提 供 **/ public class Main { public static void main(String[] args){ ArrayList<String> al = new ArrayList<String>(); al.add("cat"); al.add("dog"); al.add("rat"); // Print the content of arrayList System.out.println("ArrayList:" + al); // Create an array of the same length as the ArrayList String[] s1 = new String[al.size()]; // Copy the ArrayList elements to the array String[] s2 = al.toArray(s1);//来自 NowJava.com - 时代Java System.out.println("s1 == s2:" + (s1 == s2)); System.out.println("s1:" + Arrays.toString(s1)); System.out.println("s2:" + Arrays.toString(s2)); // Create an array of string with 1 element. s1 = new String[1]; s1[0] = "hello" ; // Store hello in first element // Copy ArrayList to the array s1