一、List列表與對象數(shù)組
List列表中存儲對象,如List<Integer>
、List<String>
、List<Person>
,對象數(shù)組中同樣存儲相應(yīng)的對象,如Integer[]、String[]、Person[],對象數(shù)組與對象List的轉(zhuǎn)換可通過如下方式實現(xiàn):
(一)對象List轉(zhuǎn)對象數(shù)組
1、toArray()方法
直接調(diào)用對象List的toArray()方法轉(zhuǎn)換為對象數(shù)組,該方法的參數(shù)是T[]
,因此需要傳入對應(yīng)的對象數(shù)組構(gòu)造函數(shù),指定數(shù)組的長度,如下所示:
1 2 3 |
|
2、Stream流的toArray()方法
通過Stream流的toArray()方法,傳入?yún)?shù)是對應(yīng)對象的構(gòu)造方法的方法引用,使用方式如下所示:
1 2 3 |
|
這個toArray()方法是Stream類下的,該方法說明如下所示:
/**
* 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);
該方法傳入一個函數(shù)式接口,該接口對應(yīng)一個方法引用,作用是創(chuàng)建一個新的指定類型和長度的數(shù)組,因此我們傳入的參數(shù)就是一個Integer[]數(shù)組的構(gòu)造方法的方法引用,最終得到的也就是一個Integer[]數(shù)組。
3、for循環(huán)
過于簡單,不再贅述。
(二)、對象數(shù)組轉(zhuǎn)對象List
1、使用Arrays.asList()
該方法通過傳入一個對象數(shù)組,最后轉(zhuǎn)換為一個對象List,如下所示:
1 2 3 |
|
asList方法傳入的參數(shù)是一個可變參數(shù),因此既可以傳入多個參數(shù),也可以傳入一個數(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()
通過Collections集合類的static方法將一個對象數(shù)組轉(zhuǎn)換為對象List,注意首先要創(chuàng)建出一個對象List,使用方式如下所示:
1 2 3 4 |
|
3、使用Stream中的Collector
JDK8之后可以使用Stream流來執(zhí)行轉(zhuǎn)換操作,通過Stream流的終結(jié)操作collect來指定將要轉(zhuǎn)換得到的List:
1 2 3 |
|
4、for循環(huán)
過于簡單,不再贅述。
二、List列表與基本數(shù)據(jù)類型數(shù)組
上面我們介紹了對象List列表與對象數(shù)組之間的轉(zhuǎn)換,但是有些情況需要直接將對象List轉(zhuǎn)換為基本數(shù)據(jù)類型數(shù)組,如List<Integer>
轉(zhuǎn)int[]
這種情況,下面詳細(xì)介紹。
(一)、對象List轉(zhuǎn)基本數(shù)據(jù)類型數(shù)組
1、Stream流執(zhí)行轉(zhuǎn)換
通過Stream流執(zhí)行轉(zhuǎn)換,如List<Integer>
轉(zhuǎn)換為int[]
,通過Stream流的mapToInt()可將每個Integer轉(zhuǎn)換為int,再輸出為int數(shù)組,如下所示:
1 2 3 4 5 6 |
|
2、for循環(huán)
過于簡單,不再贅述。
(二)、基本數(shù)據(jù)類型數(shù)組轉(zhuǎn)對象List
1、Stream流轉(zhuǎn)換
以int[]數(shù)組來舉例,通過Stream流的mapToObj()方法先將int[]數(shù)組中每個int值轉(zhuǎn)換為Integer包裝類,再通過collect執(zhí)行終結(jié)操作轉(zhuǎn)換為Integer的List。
int[] integersArray = {1, 2, 3};
// 1、Stream流轉(zhuǎn)換, 需要先用mapToObj進(jìn)行轉(zhuǎn)換
List<Integer> integersList = Arrays.stream(integersArray).mapToObj(Integer::new).collect(Collectors.toList());
2、for循環(huán)
for循環(huán)是最簡單、好用的方式,不再贅述。
注意,二維數(shù)組中的 list.toArray(array) 方法不能用于一維的 int[] 中。文章來源:http://www.zghlxwxcb.cn/news/detail-604864.html
因為 toArray() 方法的參數(shù)是范型對象,而 int 是標(biāo)準(zhǔn)數(shù)據(jù)類型。可以用?Interger[]來實現(xiàn)文章來源地址http://www.zghlxwxcb.cn/news/detail-604864.html
到了這里,關(guān)于Java中List與數(shù)組之間的相互轉(zhuǎn)換的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!