欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java將集合元素轉(zhuǎn)換為數(shù)組的三種方式

 更新時(shí)間:2025年11月06日 11:14:33   作者:Cache技術(shù)分享  
在實(shí)際開(kāi)發(fā)中,雖然用集合來(lái)存儲(chǔ)元素通常更靈活,但有些場(chǎng)景下,我們?nèi)匀恍枰獙⒓限D(zhuǎn)換成數(shù)組,下面就來(lái)介紹一下Java將集合元素轉(zhuǎn)換為數(shù)組的三種方式,感興趣的可以了解一下

在實(shí)際開(kāi)發(fā)中,雖然用集合(Collection)來(lái)存儲(chǔ)元素通常更靈活,但有些場(chǎng)景下,我們仍然需要將集合轉(zhuǎn)換成數(shù)組(比如與老舊 API 交互、需要固定大小存儲(chǔ)等)。

Java 的 Collection 接口提供了 三種方式 來(lái)實(shí)現(xiàn)這一點(diǎn),都是通過(guò) toArray() 方法的不同重載實(shí)現(xiàn)的。

?? 三種toArray()使用方式

方法返回特點(diǎn)
toArray()Object[]返回一個(gè)對(duì)象數(shù)組,不帶類(lèi)型信息
toArray(T[] array)T[]返回一個(gè)指定類(lèi)型的數(shù)組,需要傳入一個(gè)數(shù)組
toArray(IntFunction<T[]> generator)T[]使用構(gòu)造器引用創(chuàng)建數(shù)組,代碼更簡(jiǎn)潔(JDK 8+)

1. ??? 使用無(wú)參toArray()

這種方式最簡(jiǎn)單:直接調(diào)用 toArray(),返回的是一個(gè) Object[] 類(lèi)型的數(shù)組。

Collection<String> strings = List.of("one", "two", "three");

Object[] array = strings.toArray();

System.out.println(Arrays.toString(array));

??? 輸出結(jié)果

[one, two, three]

?? 注意: 這種方式得到的是 Object[],如果你強(qiáng)制轉(zhuǎn)換成 String[],可能會(huì)在運(yùn)行時(shí)拋出異常ClassCastException)。 因此,一般不推薦在需要明確類(lèi)型時(shí)使用這種方式。

2. ??? 使用帶數(shù)組參數(shù)的toArray(T[] array)

這種方式可以直接得到正確類(lèi)型的數(shù)組,比如 String[],而且避免類(lèi)型轉(zhuǎn)換出錯(cuò)

Collection<String> strings = List.of("one", "two");

// 方式1:傳入一個(gè)長(zhǎng)度為0的數(shù)組
String[] result1 = strings.toArray(new String[0]);
System.out.println(Arrays.toString(result1));

// 方式2:傳入一個(gè)足夠大的數(shù)組
String[] largerArray = new String[5];
String[] result2 = strings.toArray(largerArray);
System.out.println(Arrays.toString(result2));

??? 輸出結(jié)果

[one, two]
[one, two, null, null, null]

? 背后細(xì)節(jié)解釋

  • 如果傳入的數(shù)組長(zhǎng)度大于等于集合的大小,元素將被直接拷貝到這個(gè)數(shù)組中;
  • 如果傳入的數(shù)組長(zhǎng)度小于集合的大小,Java會(huì)創(chuàng)建一個(gè)新數(shù)組,大小正好適配集合元素;
  • 如果數(shù)組比集合大,拷貝完元素后,第一個(gè)空余位置會(huì)設(shè)為 null,剩下的位置保持原值

?? 示例:傳入比集合大的數(shù)組

Collection<String> strings = List.of("one", "two");

String[] largerTab = {"old1", "old2", "old3", "old4"};
System.out.println("Before: " + Arrays.toString(largerTab));

String[] result = strings.toArray(largerTab);

System.out.println("After : " + Arrays.toString(result));
System.out.println("Same array? " + (result == largerTab));

??? 輸出結(jié)果

Before: [old1, old2, old3, old4]
After : [one, two, null, old4]
Same array? true

? 注意:返回的是傳入的原數(shù)組,而不是新建的數(shù)組!

3. ??? 使用toArray(IntFunction<T[]> generator)(推薦)

Java 11 開(kāi)始,你可以使用構(gòu)造器引用(例如 String[]::new)來(lái)創(chuàng)建數(shù)組,更加優(yōu)雅、簡(jiǎn)潔!

Collection<String> strings = List.of("one", "two", "three");

String[] array = strings.toArray(String[]::new);

System.out.println(Arrays.toString(array));

??? 輸出結(jié)果

[one, two, three]

?? 小結(jié)

方法優(yōu)點(diǎn)缺點(diǎn)
toArray()簡(jiǎn)單直接只返回 Object[],需要小心類(lèi)型
toArray(T[] array)返回指定類(lèi)型,兼容老版本Java代碼稍長(zhǎng),需要處理數(shù)組長(zhǎng)度
toArray(IntFunction<T[]> generator)代碼最簡(jiǎn)潔,推薦(Java 11+)需要理解構(gòu)造器引用語(yǔ)法

到此這篇關(guān)于Java將集合元素轉(zhuǎn)換為數(shù)組的三種方式的文章就介紹到這了,更多相關(guān)Java 集合元素轉(zhuǎn)換為數(shù)組內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論