1. 使用`toArray()`方法:
Collection
collection = new ArrayList<>(); // 添加元素到集合
collection.add(1);
collection.add(2);
collection.add(3);
// 将集合转换为Integer数组
Integer[] array = collection.toArray(new Integer);
2. 使用`toArray(T[] a)`方法:
Collection
collection = new LinkedList<>(); // 添加元素到集合
collection.add("a");
collection.add("b");
collection.add("c");
// 将集合转换为String数组,并指定数组大小
String[] array = collection.toArray(new String);
3. 使用Java 8的流API和`toArray()`方法:
List
list = new ArrayList<>(); // 添加元素到集合
list.add("Java");
list.add("Python");
list.add("C++");
// 使用流API将集合转换为数组
String[] array = list.stream().toArray(String[]::new);