This method returns an array containing all of the elements in the collection in the same order as the original collection. The returned array is an Object array, which means that the type of the elements in the array is not specified. The size of the returned array is equal to the size of the collection.

Syntax:

public Object[] toArray()

Example:

List<String> list = new ArrayList<>();
list.add('apple');
list.add('banana');
list.add('orange');

Object[] array = list.toArray();

for (Object obj : array) {
    System.out.println(obj); // prints 'apple', 'banana', 'orange'
}
Java Collection toArray() Method: Convert Collection to Array

原文地址: https://www.cveoy.top/t/topic/n0zf 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录