国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

List集合轉(zhuǎn)換成數(shù)組list.toArray

這篇具有很好參考價(jià)值的文章主要介紹了List集合轉(zhuǎn)換成數(shù)組list.toArray。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

? ? ? ? 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。

? ? ? ? 所以終于解開(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)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Java:List相互轉(zhuǎn)換數(shù)組

    經(jīng)常我們會(huì)遇到前端傳服務(wù)端值為數(shù)組的時(shí)候我們需要對(duì)其轉(zhuǎn)換成集合便于一些其它操作,刪除,匹配等操作,今天我們就總結(jié)下數(shù)組集合相互轉(zhuǎn)換的方法 1、Object[] objArray = arrayList.toArray(); 2、String[] strArray = new String[list.size()]; 3、String[] strArray = list.toArray(new String[list.size()])

    2024年01月18日
    瀏覽(30)
  • Java中List與數(shù)組之間的相互轉(zhuǎn)換

    List列表中存儲(chǔ)對(duì)象,如 ListInteger 、 ListString 、 ListPerson ,對(duì)象數(shù)組中同樣存儲(chǔ)相應(yīng)的對(duì)象,如Integer[]、String[]、Person[],對(duì)象數(shù)組與對(duì)象List的轉(zhuǎn)換可通過(guò)如下方式實(shí)現(xiàn): (一)對(duì)象List轉(zhuǎn)對(duì)象數(shù)組 1、toArray()方法 直接調(diào)用對(duì)象List的toArray()方法轉(zhuǎn)換為對(duì)象數(shù)組,該方法的參數(shù)是

    2024年02月16日
    瀏覽(29)
  • Java 中數(shù)組Array和列表List的轉(zhuǎn)換

    主要介紹Java中Java 中數(shù)組Array和列表List的轉(zhuǎn)換。 1.使用Collections.addAll()方法 使用Collections.addAll()方法,返回的List可以執(zhí)行新增add方法,但該種方式只針對(duì)引用對(duì)象,不針對(duì)基本數(shù)據(jù)類型,該種方法效率較高,推薦用法。 2.使用new ArrayList()構(gòu)造器方法 new ArrayList()構(gòu)造器可以傳入

    2024年02月10日
    瀏覽(24)
  • Java基礎(chǔ)_集合類_List

    Java基礎(chǔ)_集合類_List

    類圖: (1)AbstractCollection Collection接口的骨架式實(shí)現(xiàn)類,最小化實(shí)現(xiàn)Collection接口的代價(jià)。 (2)AbstractList List接口的骨架式實(shí)現(xiàn)類,最小化實(shí)現(xiàn)List接口的代價(jià)。**“隨機(jī)訪問(wèn)”**數(shù)據(jù)存儲(chǔ)。 提供了iterator()、listIterator()方法的實(shí)現(xiàn)。 重要屬性 : protected transient int modCount;【 修改

    2024年04月28日
    瀏覽(23)
  • 【Java基礎(chǔ)】Java中List集合的常用方法

    在Java編程中,List集合是最常用的一種數(shù)據(jù)結(jié)構(gòu)之一。它具有動(dòng)態(tài)擴(kuò)容、元素添加、刪除和查詢等基礎(chǔ)操作,可以存儲(chǔ)各種類型的對(duì)象,并且支持泛型。在本文中,我將介紹Java List集合的常用方法,并通過(guò)實(shí)例演示這些方法的使用。 一、List集合的創(chuàng)建與初始化 在使用List集合

    2024年02月16日
    瀏覽(21)
  • java將list轉(zhuǎn)為逗號(hào)隔開(kāi)字符串,將逗號(hào)連接的字符串轉(zhuǎn)成字符數(shù)組,?將逗號(hào)分隔的字符串轉(zhuǎn)換為L(zhǎng)ist?(Java逗號(hào)分隔-字符串與數(shù)組相互轉(zhuǎn)換)

    java將list轉(zhuǎn)為逗號(hào)隔開(kāi)字符串,將逗號(hào)連接的字符串轉(zhuǎn)成字符數(shù)組,?將逗號(hào)分隔的字符串轉(zhuǎn)換為L(zhǎng)ist?(Java逗號(hào)分隔-字符串與數(shù)組相互轉(zhuǎn)換)

    ? ?參考:java將list轉(zhuǎn)為逗號(hào)隔開(kāi)字符串_51CTO博客_list轉(zhuǎn)字符串逗號(hào)隔開(kāi) Java將字符串轉(zhuǎn)化為數(shù)組_java 字符串轉(zhuǎn)數(shù)組-CSDN博客? Java逗號(hào)分隔-字符串與數(shù)組相互轉(zhuǎn)換-CSDN博客 ?

    2024年02月08日
    瀏覽(43)
  • java基礎(chǔ) -02java集合之 List,AbstractList,ArrayList介紹

    java基礎(chǔ) -02java集合之 List,AbstractList,ArrayList介紹

    在正式List之前,我們先了解我們補(bǔ)充上篇Collection接口的拓展實(shí)現(xiàn),也就是說(shuō)當(dāng)我我們需要實(shí)現(xiàn)一個(gè)不可修改的Collection的時(shí)候,我們只需要拓展某個(gè)類,也就是AbstractCollection這個(gè)類,他是Collection接口的骨干實(shí)現(xiàn),并以最大限度的實(shí)現(xiàn)了減少此接口所需要的工作; 如上兩圖進(jìn)行

    2024年01月20日
    瀏覽(24)
  • 把list集合轉(zhuǎn)換成另一個(gè)list集合的三個(gè)方法

    1.把list集合轉(zhuǎn)換成另一個(gè)list集合方法1,使用jdk1.8流 ListModelInputNode.ModelColumns modelColumns=standardTableOutPutNode.getData().getColumns().stream(). ?? ??? ?.map(column-new ModelInputNode.ModelColumns(UUID.randomUUID().toString(),column.getSourceColumn(),column.getType2(),1)).collect(Collectors.toList()); 2.把list集合轉(zhuǎn)換成另

    2024年02月09日
    瀏覽(20)
  • Java基礎(chǔ)六 - Collection集合List、Set、Queue,Map

    1. List - ArrayList、LinkedList、Vector ArrayList ????????2. LinkedList ????????3.?Vector ????????4. 常見(jiàn)使用方法 2. Set - HashSet、LinkedHashSet、TreeSet 1.?HashSet 2. LinkedHashSet 3. TreeSet 4. 常用方法 3. Map -?HashMap、TreeMap、LinkedHashMap、Hashtable 1.?HashMap 2.?LinkedHashMap 3.?TreeMap 4.?Hashtable 5.

    2024年02月14日
    瀏覽(28)
  • 對(duì)List集合、數(shù)組去重

    還記得在2021我發(fā)布的第一篇博客就是關(guān)于數(shù)組的去重,從那一刻開(kāi)始,命運(yùn)的齒輪開(kāi)始轉(zhuǎn)動(dòng)…… 扯遠(yuǎn)了哈哈哈,我重新寫這篇文章只是想讓去重方式更加嚴(yán)謹(jǐn)(ps:我才不會(huì)說(shuō)是因?yàn)榧夹g(shù)成長(zhǎng)了,看不上之前寫的了哈哈哈 ?依據(jù)Set集合的特性,使用set去重(最簡(jiǎn)潔高效) ?使

    2024年02月14日
    瀏覽(29)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包