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

Java中List與數(shù)組之間的相互轉(zhuǎn)換

這篇具有很好參考價值的文章主要介紹了Java中List與數(shù)組之間的相互轉(zhuǎn)換。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

一、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

ArrayList<Integer> integersList = new ArrayList<>(Arrays.asList(1,2,3));

// 1、toArray()方法

Integer[] integersArrau = integersList.toArray(new Integer[integersList.size()]);

// 2、toArray()方法,等價于上面的用法,即0會自動替換為size()

Integer[] integersArrau = integersList.toArray(new Integer[0]);

???????

2、Stream流的toArray()方法

通過Stream流的toArray()方法,傳入?yún)?shù)是對應(yīng)對象的構(gòu)造方法的方法引用,使用方式如下所示:

1

2

3

ArrayList<Integer> integersList = new ArrayList<>(Arrays.asList(1,2,3));

// 2、Stream流的toArray()方法

Integer[] integersArray2 = integersList.stream().toArray(Integer[]::new);

這個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

Integer[] integersArray = {1, 2, 3};

// 1、使用Arrays.asList()

List<Integer> integersList = Arrays.asList(integersArray);

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&lt;String&gt; 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

Integer[] integersArray = {1, 2, 3};

// 2、使用Collections.addAll()

ArrayList<Integer> integersList2 = new ArrayList<>();

Collections.addAll(integersList2,integersArray);

3、使用Stream中的Collector

JDK8之后可以使用Stream流來執(zhí)行轉(zhuǎn)換操作,通過Stream流的終結(jié)操作collect來指定將要轉(zhuǎn)換得到的List:

1

2

3

Integer[] integersArray = {1, 2, 3};

// 3、使用Stream中的Collector

List<Integer> integersList3 = Arrays.stream(integersArray).collect(Collectors.toList());

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

ArrayList<Integer> integersList = new ArrayList<>(Arrays.asList(1,2,3));

// 1、Stream流執(zhí)行轉(zhuǎn)換

// 方法引用

int[] arrays1 = integersList.stream().mapToInt(Integer::intValue).toArray();? //需要先用mapToInt進(jìn)行轉(zhuǎn)換

// 2、 lambda表達(dá)式

int[] arrays2 = integersList.stream().mapToInt(i -> i).toArray();

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[] 中。

因為 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)!

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

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

相關(guān)文章

  • java將list轉(zhuǎn)為逗號隔開字符串,將逗號連接的字符串轉(zhuǎn)成字符數(shù)組,?將逗號分隔的字符串轉(zhuǎn)換為List?(Java逗號分隔-字符串與數(shù)組相互轉(zhuǎn)換)

    java將list轉(zhuǎn)為逗號隔開字符串,將逗號連接的字符串轉(zhuǎn)成字符數(shù)組,?將逗號分隔的字符串轉(zhuǎn)換為List?(Java逗號分隔-字符串與數(shù)組相互轉(zhuǎn)換)

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

    2024年02月08日
    瀏覽(43)
  • Python中列表(list)與數(shù)組(array)的相互轉(zhuǎn)換方法介紹

    在Python編程中,列表(list)和數(shù)組(array)是常見的數(shù)據(jù)結(jié)構(gòu),它們在存儲和處理數(shù)據(jù)時具有不同的特點和用途。有時候我們需要在列表和數(shù)組之間進(jìn)行相互轉(zhuǎn)換。本文將介紹如何在Python中實現(xiàn)列表與數(shù)組之間的相互轉(zhuǎn)換,并提供相應(yīng)的源代碼示例。 列表(list)轉(zhuǎn)換為數(shù)組

    2024年02月05日
    瀏覽(26)
  • 每日一道面試題之如何實現(xiàn)數(shù)組和 List 之間的轉(zhuǎn)換?

    要實現(xiàn)數(shù)組和List之間的轉(zhuǎn)換,可以使用Java中的 Arrays類 和 Collections類 提供的方法。 數(shù)組轉(zhuǎn)換為List: 使用 Arrays類 的 asList()方法 可以 將數(shù)組轉(zhuǎn)換為List 。這個方法接受一個數(shù)組作為參數(shù),并返回一個包含數(shù)組元素固定大小的List。 舉例: 輸出如下所示: List轉(zhuǎn)換為數(shù)組: 使

    2024年02月16日
    瀏覽(27)
  • Java中List,Set,數(shù)組Arrays相互轉(zhuǎn)化

    很多場合需要進(jìn)行轉(zhuǎn)換( 例如力扣中 ) 數(shù)組轉(zhuǎn)換其他時比較容易,反過來就需要操作一番 以下轉(zhuǎn)換的方法并不唯一,但確保簡潔易懂 常規(guī)的方法:從數(shù)組中拿出元素放進(jìn)list 用工具類或者庫函數(shù): 這個比較容易,從數(shù)組中拿出元素放進(jìn)set(同時會自動去重) 這個需要操作

    2024年02月11日
    瀏覽(29)
  • java byte數(shù)組與int之間相互轉(zhuǎn)換

    運算符 含義 說明 與 對應(yīng)位都是1,結(jié)果為1,否則為0 | 或 對應(yīng)位都是0,結(jié)果為0,否則為1 ~ 取反 每一位變相反位,即0變成1,1變成0 ^ 異或 對應(yīng)位值相同,結(jié)果為0,否則為1 左移位 低位補0 右移位 保留符號位,0為正,1為負(fù) 無符號右移位 高位補0 位邏輯運算示例 A B AB A|B

    2024年04月14日
    瀏覽(123)
  • Java中的List<T>對象與Json格式的字符串的相互轉(zhuǎn)換

    在這里我隨便舉一個例子 OK,以上就是互相轉(zhuǎn)換的過程 我使用的場景是在訂單的訂單列表項這里,涉及到數(shù)據(jù)庫相應(yīng)字段數(shù)據(jù)的存放與提取,我的做法是,將List轉(zhuǎn)換為Json格式字符串存入,取時再將Json格式轉(zhuǎn)為List

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

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

    2024年02月10日
    瀏覽(24)
  • 【Java】集合List轉(zhuǎn)換為數(shù)組【toArray() /stream()流】實現(xiàn)

    在Java中,集合(List 接口的實現(xiàn)類)提供了一個名為 toArray 的方法,用于將集合中的元素轉(zhuǎn)換成數(shù)組。該方法有兩個主要的重載形式,分別用于不同的情況。 這個方法將集合中的元素復(fù)制到一個指定類型的數(shù)組中,并返回該數(shù)組。 如果指定的數(shù)組大小足夠容納集合中的所有

    2024年02月03日
    瀏覽(19)
  • 【Python】數(shù)據(jù)框DataFrame和列表List相互轉(zhuǎn)換

    【Python】數(shù)據(jù)框DataFrame和列表List相互轉(zhuǎn)換

    在使用一些別人封裝好的庫的時候,調(diào)用函數(shù)返回的結(jié)果便是DataFrame,這時如果要對內(nèi)部數(shù)據(jù)做一些加工處理的話會很不方便。我們要需要將DataFrame還原成列表的形式來處理。 ? ? 列表轉(zhuǎn)數(shù)據(jù)框根據(jù)需要有3中轉(zhuǎn)換方式 執(zhí)行結(jié)果: ? ?0 0 ?A 1 ?B 2 ?C 執(zhí)行結(jié)果: ? ? ? ? ? ?

    2024年02月11日
    瀏覽(20)
  • List集合轉(zhuǎn)換成數(shù)組list.toArray

    ? ? ? ? List集合轉(zhuǎn)換成集合,List類本身提供了兩個api: ????????一個是把集合轉(zhuǎn)換成元素數(shù)據(jù)類型為Object的數(shù)組;另外一個則是一個泛型函數(shù)。其中泛型函數(shù)這個api是比較常用的,因為它轉(zhuǎn)換后得到的數(shù)組的元素類型仍然是列表中的數(shù)據(jù)元素類型,而不是Object類型。 ? ?

    2024年02月10日
    瀏覽(23)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

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

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包