Java中List與數(shù)組之間的相互轉(zhuǎn)換
一、前言
在Java編碼中,我們經(jīng)常會(huì)遇到List與數(shù)組的轉(zhuǎn)換,包括對(duì)象List與對(duì)象數(shù)組的轉(zhuǎn)換,以及對(duì)象List與基本數(shù)據(jù)類型數(shù)組的轉(zhuǎn)換,下面詳細(xì)介紹多種轉(zhuǎn)換方式。
二、List列表與對(duì)象數(shù)組
List列表中存儲(chǔ)對(duì)象,如List<Integer>、List<String>、List<Person>,對(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ù)是T[],因此需要傳入對(duì)應(yīng)的對(duì)象數(shù)組構(gòu)造函數(shù),指定數(shù)組的長(zhǎng)度,如下所示:
ArrayList<Integer> integersList = new ArrayList<>(Arrays.asList(1,2,3)); // 1、toArray()方法 Integer[] integersArrau = integersList.toArray(new Integer[integersList.size()]);
2、Stream流的toArray()方法
通過(guò)Stream流的toArray()方法,傳入?yún)?shù)是對(duì)應(yīng)對(duì)象的構(gòu)造方法的方法引用,使用方式如下所示:
ArrayList<Integer> integersList = new ArrayList<>(Arrays.asList(1,2,3)); // 2、Stream流的toArray()方法 Integer[] integersArray2 = integersList.stream().toArray(Integer[]::new);
這個(gè)toArray()方法是Stream類下的,該方法說(shuō)明如下所示:
/**
* Returns an array containing the elements of this stream, using the
* provided {@code generator} function to allocate the returned array, as
* well as any additional arrays that might be required for a partitioned
* execution or for resizing.
*
* <p>This is a <a href="package-summary.html#StreamOps" rel="external nofollow" >terminal
* operation</a>.
*
* @apiNote
* The generator function takes an integer, which is the size of the
* desired array, and produces an array of the desired size. This can be
* concisely expressed with an array constructor reference:
* <pre>{@code
* Person[] men = people.stream()
* .filter(p -> p.getGender() == MALE)
* .toArray(Person[]::new);
* }</pre>
*
* @param <A> the element type of the resulting array
* @param generator a function which produces a new array of the desired
* type and the provided length
* @return an array containing the elements in this stream
* @throws ArrayStoreException if the runtime type of the array returned
* from the array generator is not a supertype of the runtime type of every
* element in this stream
*/
<A> A[] toArray(IntFunction<A[]> generator);該方法傳入一個(gè)函數(shù)式接口,該接口對(duì)應(yīng)一個(gè)方法引用,作用是創(chuàng)建一個(gè)新的指定類型和長(zhǎng)度的數(shù)組,因此我們傳入的參數(shù)就是一個(gè)Integer[]數(shù)組的構(gòu)造方法的方法引用,最終得到的也就是一個(gè)Integer[]數(shù)組。
3、for循環(huán)
過(guò)于簡(jiǎn)單,不再贅述。
(二)、對(duì)象數(shù)組轉(zhuǎn)對(duì)象List
1、使用Arrays.asList()
該方法通過(guò)傳入一個(gè)對(duì)象數(shù)組,最后轉(zhuǎn)換為一個(gè)對(duì)象List,如下所示:
Integer[] integersArray = {1, 2, 3};
// 1、使用Arrays.asList()
List<Integer> integersList = Arrays.asList(integersArray);asList方法傳入的參數(shù)是一個(gè)可變參數(shù),因此既可以傳入多個(gè)參數(shù),也可以傳入一個(gè)數(shù)組,如下所示:
/**
* Returns a fixed-size list backed by the specified array. (Changes to
* the returned list "write through" to the array.) This method acts
* as bridge between array-based and collection-based APIs, in
* combination with {@link Collection#toArray}. The returned list is
* serializable and implements {@link RandomAccess}.
*
* <p>This method also provides a convenient way to create a fixed-size
* list initialized to contain several elements:
* <pre>
* List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
* </pre>
*
* @param <T> the class of the objects in the array
* @param a the array by which the list will be backed
* @return a list view of the specified array
*/
@SafeVarargs
@SuppressWarnings("varargs")
public static <T> List<T> asList(T... a) {
return new ArrayList<>(a);
}2、使用Collections.addAll()
通過(guò)Collections集合類的static方法將一個(gè)對(duì)象數(shù)組轉(zhuǎn)換為對(duì)象List,注意首先要?jiǎng)?chuàng)建出一個(gè)對(duì)象List,使用方式如下所示:
Integer[] integersArray = {1, 2, 3};
// 2、使用Collections.addAll()
ArrayList<Integer> integersList2 = new ArrayList<>();
Collections.addAll(integersList2,integersArray);3、使用Stream中的Collector
JDK8之后可以使用Stream流來(lái)執(zhí)行轉(zhuǎn)換操作,通過(guò)Stream流的終結(jié)操作collect來(lái)指定將要轉(zhuǎn)換得到的List:
Integer[] integersArray = {1, 2, 3};
// 3、使用Stream中的Collector
List<Integer> integersList3 = Arrays.stream(integersArray).collect(Collectors.toList());4、for循環(huán)
過(guò)于簡(jiǎn)單,不再贅述。
三、List列表與基本數(shù)據(jù)類型數(shù)組
上面我們介紹了對(duì)象List列表與對(duì)象數(shù)組之間的轉(zhuǎn)換,但是有些情況需要直接將對(duì)象List轉(zhuǎn)換為基本數(shù)據(jù)類型數(shù)組,如List<Integer>轉(zhuǎn)int[]這種情況,下面詳細(xì)介紹。
(一)、對(duì)象List轉(zhuǎn)基本數(shù)據(jù)類型數(shù)組
1、Stream流執(zhí)行轉(zhuǎn)換
通過(guò)Stream流執(zhí)行轉(zhuǎn)換,如List<Integer>轉(zhuǎn)換為int[],通過(guò)Stream流的mapToInt()可將每個(gè)Integer轉(zhuǎn)換為int,再輸出為int數(shù)組,如下所示:
ArrayList<Integer> integersList = new ArrayList<>(Arrays.asList(1,2,3)); // 1、Stream流執(zhí)行轉(zhuǎn)換 // 方法引用 int[] arrays1 = integersList.stream().mapToInt(Integer::intValue).toArray(); // lambda表達(dá)式 int[] arrays2 = integersList.stream().mapToInt(i -> i).toArray();
2、for循環(huán)
過(guò)于簡(jiǎn)單,不再贅述。
(二)、基本數(shù)據(jù)類型數(shù)組轉(zhuǎn)對(duì)象List
1、Stream流轉(zhuǎn)換
以int[]數(shù)組來(lái)舉例,通過(guò)Stream流的mapToObj()方法先將int[]數(shù)組中每個(gè)int值轉(zhuǎn)換為Integer包裝類,再通過(guò)collect執(zhí)行終結(jié)操作轉(zhuǎn)換為Integer的List。
int[] integersArray = {1, 2, 3};
// 1、Stream流轉(zhuǎn)換
List<Integer> integersList = Arrays.stream(integersArray).mapToObj(Integer::new).collect(Collectors.toList());2、for循環(huán)
for循環(huán)是最簡(jiǎn)單、好用的方式,不再贅述。
注意,二維數(shù)組中的 list.toArray(array) 方法不能用于一維的 int[] 中。
因?yàn)?toArray() 方法的參數(shù)是范型對(duì)象,而 int 是標(biāo)準(zhǔn)數(shù)據(jù)類型??梢杂?nbsp;Interger[]來(lái)實(shí)現(xiàn)
總結(jié)
到此這篇關(guān)于Java中List與數(shù)組之間的相互轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)Java List與數(shù)組相互轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java-collection中的null,isEmpty用法
這篇文章主要介紹了java-collection中的null,isEmpty用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
Java SpringBoot整合Canal實(shí)現(xiàn)數(shù)據(jù)同步方式
本文介紹了如何開啟和配置Canal,以及如何在Spring Boot中集成Canal,Canal是一種基于MySQL的數(shù)據(jù)庫(kù)變更解析工具,可以將數(shù)據(jù)庫(kù)的變更事件發(fā)送到Kafka、RocketMQ等消息隊(duì)列中,用于數(shù)據(jù)分析和挖掘2025-02-02
Idea Jrebel 報(bào)錯(cuò):Cannot reactivate,offline 
本文主要介紹了Idea Jrebel 報(bào)錯(cuò):Cannot reactivate,offline seat in use,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
Spring?Initializr只能創(chuàng)建為Java?17版本以上的問(wèn)題解決
這篇文章主要給大家介紹了關(guān)于Spring?Initializr只能創(chuàng)建為Java?17版本以上問(wèn)題的解決辦法,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-01-01
SpringBoot集成mybatis連接oracle的圖文教程
這篇文章主要介紹了Spring Boot集成mybatis連接oracle的圖文教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
java 獲取日期的幾天前,幾個(gè)月前和幾年前的實(shí)例
下面小編就為大家?guī)?lái)一篇java 獲取日期的幾天前,幾個(gè)月前和幾年前的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10

