List 转 Array
有两个方法,下面分别介绍。
Object[] toArray()
用法很简单。
返回一个Object
类型的新建数组,大小和顺序与List相同。
T[] toArray(T[] a)
传入一个同类型的数组,返回一个数组。
那么传入的数组要多大呢?返回的数组也是新建的吗?
查询java api:
Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.
If the list fits in the specified array with room to spare (i.e., the array ha