? ? ? ? List集合轉(zhuǎn)換成集合,List類本身提供了兩個(gè)api:
Object[] toArray();
<T> T[] toArray(T[] a);
????????一個(gè)是把集合轉(zhuǎn)換成元素?cái)?shù)據(jù)類型為Object的數(shù)組;另外一個(gè)則是一個(gè)泛型函數(shù)。其中泛型函數(shù)這個(gè)api是比較常用的,因?yàn)樗D(zhuǎn)換后得到的數(shù)組的元素類型仍然是列表中的數(shù)據(jù)元素類型,而不是Object類型。
? ? ? ? 在實(shí)際使用中,它的用法如下:
List<String> list = new ArrayList<>();
list.add("xxx1");
list.add("xxx2");
list.add("xxx3");
String[] strings = list.toArray(new String[0]);
for (String str : strings) {
out.println(str);
}
????????注意到toArray()函數(shù)傳入的實(shí)參為new String[0]。代表一個(gè)數(shù)組長(zhǎng)度為0的字符串?dāng)?shù)組。在java中允許數(shù)組長(zhǎng)度為0。這一點(diǎn)在編寫一個(gè)結(jié)果為數(shù)組的方法時(shí),如果碰巧結(jié)果為空時(shí),new String[0]這種寫法就非常有用了。 在這里我們主要是利用了字符串?dāng)?shù)組的數(shù)據(jù)類型信息,并沒(méi)有實(shí)際地開(kāi)辟一塊內(nèi)存,這樣就不會(huì)浪費(fèi)內(nèi)存了。 那么list.toArray(new String[0])是如何做到傳入一個(gè)長(zhǎng)度為0 的數(shù)組,返回的卻是list集合的數(shù)組形式呢?我們來(lái)看這個(gè)函數(shù)的實(shí)現(xiàn):
/**
* 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.
*
* <p>If the list fits in the specified array with room to spare (i.e.,
* the array has more elements than the list), the element in the array
* immediately following the end of the list is set to <tt>null</tt>.
* (This is useful in determining the length of the list <i>only</i> if
* the caller knows that the list does not contain any null elements.)
*
* <p>Like the {@link #toArray()} method, this method acts as bridge between
* array-based and collection-based APIs. Further, this method allows
* precise control over the runtime type of the output array, and may,
* under certain circumstances, be used to save allocation costs.
*
* <p>Suppose <tt>x</tt> is a list known to contain only strings.
* The following code can be used to dump the list into a newly
* allocated array of <tt>String</tt>:
*
* <pre>{@code
* String[] y = x.toArray(new String[0]);
* }</pre>
*
* Note that <tt>toArray(new Object[0])</tt> is identical in function to
* <tt>toArray()</tt>.
*
* @param a the array into which the elements of this list are to
* be stored, if it is big enough; otherwise, a new array of the
* same runtime type is allocated for this purpose.
* @return an array containing the elements of this list
* @throws ArrayStoreException if the runtime type of the specified array
* is not a supertype of the runtime type of every element in
* this list
* @throws NullPointerException if the specified array is null
*/
<T> T[] toArray(T[] a);
我們來(lái)看其在ArrayList上的具體實(shí)現(xiàn):
public <T> T[] toArray(T[] a) {
if (a.length < size)
// Make a new array of a's runtime type, but my contents:
return (T[]) Arrays.copyOf(elementData, size, a.getClass());
System.arraycopy(elementData, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}
? ? ? ? 首先比較形參數(shù)組a的數(shù)組長(zhǎng)度和列表的長(zhǎng)度,如果a.length<size,則調(diào)用Arrays.copyOf(elementData, size, a.getClass()),其內(nèi)部實(shí)現(xiàn)為:
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
@SuppressWarnings("unchecked")
T[] copy = ((Object)newType == (Object)Object[].class)
? (T[]) new Object[newLength]
: (T[]) Array.newInstance(newType.getComponentType(), newLength);
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
????????可以看到其內(nèi)部會(huì)開(kāi)辟出一塊和列表長(zhǎng)度一樣的數(shù)組空間大小,并且數(shù)組元素類型和傳進(jìn)來(lái)的元素類型一致。調(diào)用System.arraycopy()函數(shù)進(jìn)行數(shù)組拷貝。
????????如果a.length>size,則不需要單獨(dú)開(kāi)辟空間了,因?yàn)樾螀⒌目臻g足夠大了。然后也是調(diào)用System.arraycopy()函數(shù)進(jìn)行數(shù)組拷貝。最后把形參數(shù)組a中剩余空間的值全部賦值為null。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-692532.html
? ? ? ? 所以終于解開(kāi)了List的toArray()函數(shù)的面紗了。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-692532.html
到了這里,關(guān)于List集合轉(zhuǎn)換成數(shù)組list.toArray的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!