1. 使用 `toArray()` 方法:
Collectioncollection = new ArrayList<>(); // 添加元素到集合collection.add(1);collection.add(2);collection.add(3);// 将集合转换为 Integer 数组Integer[] array = collection.toArray(new Integer);
2. 使用 `toArray(T[] a)` 方法:
Collectioncollection = new LinkedList<>(); // 添加元素到集合collection.add("a");collection.add("b");collection.add("c");// 将集合转换为 String 数组,并指定数组大小String[] array = collection.toArray(new String);

3. 使用Java 8引入的流(Streams)API:
Listlist = new ArrayList<>(); // 添加元素到集合list.add("apple");list.add("banana");list.add("cherry");// 使用流将集合转换为数组T[] array = list.stream().toArray(size -> new T[size]);
请注意,`toArray()` 方法返回的数组类型取决于集合中元素的类型。如果集合中的元素类型与指定数组类型不匹配,你可能需要进行类型转换。
希望这些信息对你有帮助!
