java Arrays.asList用法


##java Arrays.asList用法

###用途

Arrays是java容器相關操作的工具類,asList方法將Array轉換為list,是Array和List之間的橋梁。

###注意

Arrays.asList返回一個基於參數array的fixed list,即不能對返回的list進行修改操作,如刪除操作、增加操作等。如果想獲得可修改的List,那么可采用如下方式操作:

new ArrayList<Integer>(Arrays.asList(arr))
注:then you create new ArrayList, which is a full, independent copy of the original one. Although here you create the wrapper using Arrays.asList as well, it is used only during the construction of the new ArrayList and is garbage-collected afterwards. The structure of this new ArrayList is completely independent of the original array. It contains the same elements (both the original array and this new ArrayList reference the same integers in memory), but it creates a new, internal array, that holds the references. So when you shuffle it, add, remove elements etc., the original array is unchanged.

new LinkedList<Integer>(Arrays.asList(arr))
注:LinkedList支持更快的remove操作。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM