最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java list與數(shù)組之間的轉(zhuǎn)換詳細解析

 更新時間:2013年09月03日 10:01:20   作者:  
以下是對java中l(wèi)ist與數(shù)組之間的轉(zhuǎn)換進行了詳細的分析介紹,需要的朋友可以過來參考下

1 數(shù)組轉(zhuǎn)換為List
調(diào)用Arrays類的靜態(tài)方法asList。

asList
public static <T> List<T> asList(T... a)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 Collection.toArray(). The returned list is serializable and implements RandomAccess.
This method also provides a convenient way to create a fixed-size list initialized to contain several elements:

List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");

Parameters:
a - the array by which the list will be backed
Returns:
a list view of the specified array

用法:API中提供了一種使用的方法。更為常用的示例代碼:

復制代碼 代碼如下:

String[] arr = new String[] {"str1", "str2"};
List<String> list = Arrays.asList(arr);

2 List轉(zhuǎn)換為數(shù)組
這里的List以ArrayList為例,ArrayList的API提供了兩種可供使用的函數(shù)。

toArray
public Object[] toArray()Returns an array containing all of the elements in this list in proper sequence (from first to last element).
The returned array will be "safe" in that no references to it are maintained by this list. (In other words, this method must allocate a new array). The caller is thus free to modify the returned array.

This method acts as bridge between array-based and collection-based APIs.

Specified by:
toArray in interface Collection<E>
Specified by:
toArray in interface List<E>
Overrides:
toArray in class AbstractCollection<E>
Returns:
an array containing all of the elements in this list in proper sequence
See Also:
Arrays.asList(Object[])

--------------------------------------------------------------------------------
toArray
public <T> T[] toArray(T[] a)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.

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 collection is set to null. (This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.)

Specified by:
toArray in interface Collection<E>
Specified by:
toArray in interface List<E>
Overrides:
toArray in class AbstractCollection<E>
Parameters:
a - the array into which the elements of the list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
Returns:
an array containing the elements of the 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
NullPointerException - if the specified array is null

用法:示例代碼:

復制代碼 代碼如下:

List<String> list = new ArrayList<String>();
list.add("str1");
list.add("str2");
int size = list.size();
String[] arr = (String[])list.toArray(new String[size]);//使用了第二種接口,返回值和參數(shù)均為結(jié)果

相關(guān)文章

  • Java報錯org.hibernate.TypeMismatchException的解決方法

    Java報錯org.hibernate.TypeMismatchException的解決方法

    在Java開發(fā)領(lǐng)域,尤其是涉及到數(shù)據(jù)持久化的項目中,Hibernate是一款廣泛使用的強大工具,然而,可能會在使用過程中遭遇各種報錯,其中org.hibernate.TypeMismatchException就是一個讓人頭疼的問題,下面我們一起深入剖析這個報錯信息
    2024-11-11
  • 淺析Java自定義注解的用法

    淺析Java自定義注解的用法

    注解為我們在代碼中添加信息提供一種形式化的方法,使我們可以在源碼、編譯時、運行時非常方便的使用這些數(shù)據(jù)。本文主要為大家介紹了Java自定義注解的用法,希望對大家有所幫助
    2023-03-03
  • POI讀取excel簡介_動力節(jié)點Java學院整理

    POI讀取excel簡介_動力節(jié)點Java學院整理

    這篇文章主要介紹了POI讀取excel簡介,詳細的介紹了什么是Apache POI和組件,有興趣的可以了解了解一下
    2017-08-08
  • 關(guān)于Rabbitmq死信隊列及延時隊列的實現(xiàn)

    關(guān)于Rabbitmq死信隊列及延時隊列的實現(xiàn)

    這篇文章主要介紹了關(guān)于Rabbitmq死信隊列及延時隊列的實現(xiàn),TTL就是消息或者隊列的過期功能,當消息過期就會進到死信隊列,死信隊列和普通隊列沒啥區(qū)別,然后我們只需要配置一個消費者來消費死信隊列里面的消息就可以了,需要的朋友可以參考下
    2023-08-08
  • Java設(shè)計模式之策略模式案例詳解

    Java設(shè)計模式之策略模式案例詳解

    策略模式(Strategy?Pattern)定義了一組同類型的算法,在不同的類中封裝起來,每種算法可以根據(jù)當前場景相互替換,從而使算法的變化獨立于使用它們的客戶端即算法的調(diào)用者
    2022-07-07
  • 30分鐘入門Java8之方法引用學習

    30分鐘入門Java8之方法引用學習

    在Java8中,我們可以直接通過方法引用來簡寫lambda表達式中已經(jīng)存在的方法,這篇文章主要介紹了30分鐘入門Java8之方法引用學習,有興趣可以了解一下。
    2017-04-04
  • log4j控制日志輸出文件名稱的兩種方式小結(jié)

    log4j控制日志輸出文件名稱的兩種方式小結(jié)

    這篇文章主要介紹了log4j控制日志輸出文件名稱的兩種方式小結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • 深入講解RocketMQ原理

    深入講解RocketMQ原理

    這篇文章主要介紹了詳解SpringBoot整合RocketMQ,RocketMQ作為一款純java、分布式、隊列模型的開源消息中間件,支持事務(wù)消息、順序消息、批量消息、定時消息、消息回溯等,需要的朋友可以參考下
    2023-07-07
  • MybatisPlus中的多表條件排序查詢

    MybatisPlus中的多表條件排序查詢

    這篇文章主要介紹了MybatisPlus中的多表條件排序查詢,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • Java使用GUI繪制線條的示例

    Java使用GUI繪制線條的示例

    這篇文章主要介紹了Java使用GUI繪制線條的示例,幫助大家更好的理解和學習java gui編程,感興趣的朋友可以了解下
    2020-09-09

最新評論

历史| 东乡族自治县| 黔江区| 弥渡县| 绥德县| 沾化县| 思南县| 安吉县| 宜川县| 鲁山县| 阜康市| 安仁县| 贵州省| 金坛市| 五台县| 京山县| 丹江口市| 辽阳县| 共和县| 定南县| 渭南市| 海门市| 将乐县| 金沙县| 昆山市| 内黄县| 揭东县| 天津市| 徐州市| 浦东新区| 抚州市| 招远市| 铜鼓县| 重庆市| 辽阳市| 原平市| 托克托县| 长垣县| 石首市| 时尚| 邯郸县|