掃二維碼與項目經(jīng)理溝通
我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
前言:

創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站設(shè)計、成都做網(wǎng)站、正定網(wǎng)絡(luò)推廣、小程序設(shè)計、正定網(wǎng)絡(luò)營銷、正定企業(yè)策劃、正定品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供正定建站搭建服務(wù),24小時服務(wù)熱線:18980820575,官方網(wǎng)址:www.cdcxhl.com
本文介紹Java中數(shù)組轉(zhuǎn)為List三種情況的優(yōu)劣對比,以及應(yīng)用場景的對比,以及程序員常犯的類型轉(zhuǎn)換錯誤原因解析。
一.最常見方式(未必最佳)
通過 Arrays.asList(strArray) 方式,將數(shù)組轉(zhuǎn)換List后,不能對List增刪,只能查改,否則拋異常。
關(guān)鍵代碼:List list = Arrays.asList(strArray);
- private void testArrayCastToListError() {
- String[] strArray = new String[2];
- List list = Arrays.asList(strArray);
- //對轉(zhuǎn)換后的list插入一條數(shù)據(jù)
- list.add("1");
- System.out.println(list);
- }
執(zhí)行結(jié)果:
- Exception in thread "main" java.lang.UnsupportedOperationException
- at java.util.AbstractList.add(AbstractList.java:148)
- at java.util.AbstractList.add(AbstractList.java:108)
- at com.darwin.junit.Calculator.testArrayCastToList(Calculator.java:19)
- at com.darwin.junit.Calculator.main(Calculator.java:44)
程序在list.add(“1”)處,拋出異常:UnsupportedOperationException。
原因解析:
Arrays.asList(strArray)返回值是java.util.Arrays類中一個私有靜態(tài)內(nèi)部類java.util.Arrays.ArrayList,它并非java.util.ArrayList類。java.util.Arrays.ArrayList類具有 set(),get(),contains()等方法,但是不具有添加add()或刪除remove()方法,所以調(diào)用add()方法會報錯。
使用場景:Arrays.asList(strArray)方式僅能用在將數(shù)組轉(zhuǎn)換為List后,不需要增刪其中的值,僅作為數(shù)據(jù)源讀取使用。
二.數(shù)組轉(zhuǎn)為List后,支持增刪改查的方式
通過ArrayList的構(gòu)造器,將Arrays.asList(strArray)的返回值由java.util.Arrays.ArrayList轉(zhuǎn)為java.util.ArrayList。
關(guān)鍵代碼:ArrayList list = new ArrayList (Arrays.asList(strArray)) ;
- private void testArrayCastToListRight() {
- String[] strArray = new String[2];
- ArrayList
list = new ArrayList (Arrays.asList(strArray)) ; - list.add("1");
- System.out.println(list);
- }
執(zhí)行結(jié)果:成功追加一個元素“1”。
- [null, null, 1]
使用場景:需要在將數(shù)組轉(zhuǎn)換為List后,對List進行增刪改查操作,在List的數(shù)據(jù)量不大的情況下,可以使用。
三.通過集合工具類Collections.addAll()方法(最高效)
通過Collections.addAll(arrayList, strArray)方式轉(zhuǎn)換,根據(jù)數(shù)組的長度創(chuàng)建一個長度相同的List,然后通過Collections.addAll()方法,將數(shù)組中的元素轉(zhuǎn)為二進制,然后添加到List中,這是最高效的方法。
關(guān)鍵代碼:
- ArrayList< String> arrayList = new ArrayList
(strArray.length); - Collections.addAll(arrayList, strArray);
測試:
- private void testArrayCastToListEfficient(){
- String[] strArray = new String[2];
- ArrayList< String> arrayList = new ArrayList
(strArray.length); - Collections.addAll(arrayList, strArray);
- arrayList.add("1");
- System.out.println(arrayList);
- }
執(zhí)行結(jié)果:同樣成功追加一個元素“1”。
- [null, null, 1]
使用場景:需要在將數(shù)組轉(zhuǎn)換為List后,對List進行增刪改查操作,在List的數(shù)據(jù)量巨大的情況下,優(yōu)先使用,可以提高操作速度。
注:附上Collections.addAll()方法源碼:
- public static
boolean addAll(Collection super T> c, T... elements) { - boolean result = false;
- for (T element : elements)
- result |= c.add(element);//result和c.add(element)按位或運算,然后賦值給result
- return result;
- }
問題解答問題:數(shù)組類型如果是整型數(shù)組,轉(zhuǎn)為List時,會報錯?
答案: 在JDK1.8環(huán)境中測試,這三種轉(zhuǎn)換方式是沒有問題的。放心使用。對于Integer[]整型數(shù)組轉(zhuǎn)List的方法和測試結(jié)果如下:
方式一:不支持增刪
- Integer[] intArray1 = new Integer[2];
- List
list1 = Arrays.asList(intArray1); - System.out.println(list1);
運行結(jié)果:
- [null, null]
方式二:支持增刪
- Integer[] intArray2 = new Integer[2];
- List
list2 = new ArrayList (Arrays.asList(intArray2)) ; - list2.add(2);
- System.out.println(list2);
運行結(jié)果:
- [null, null, 2]
方式三:支持增刪,且數(shù)據(jù)量大最高效
- Integer[] intArray3 = new Integer[2];
- List
list3 = new ArrayList (intArray3.length); - Collections.addAll(list3, intArray3);
- list3.add(3);
- System.out.println(list3);
運行結(jié)果:
- [null, null, 3]
綜上,整型Integer[]數(shù)組轉(zhuǎn)List 的正確方式應(yīng)該是這樣的。
易錯點:可能出現(xiàn)的錯誤可能是這樣轉(zhuǎn)換的:
- int[] intArray1 = new int[2];
- List
list1 = Arrays.asList(intArray1);//此處報錯?。?!
報錯原因:等號兩邊類型不一致,當(dāng)然編譯不通過。分析見下文。
那么在聲明數(shù)組時,用int[] 還是Integer[],哪種聲明方式才能正確的轉(zhuǎn)為List呢?
答案: 只能用Integer[]轉(zhuǎn)List ,即只能用基本數(shù)據(jù)類型的包裝類型,才能直接轉(zhuǎn)為List。
原因分析如下:
我們來看List在Java源碼中的定義(別害怕看不懂源碼,看我分析,很易懂的):
- public interface List
extends Collection {省略…}
再來看Arrays.asList()的在Java源碼定義:
- public static List asList(T... a) {
- return new ArrayList<>(a);
- }
有了上述基礎(chǔ)知識后,再來看為什么下面兩行代碼第二行能編譯通過,第三行卻編譯報錯?
- int[] intArray1 = new int[1];
- Arrays.asList(intArray1);//編譯不報錯
- List
list1 = Arrays.asList( intArray1);//編譯報錯
答案:
總結(jié)
現(xiàn)在你應(yīng)該明白,為什么int[]不能直接轉(zhuǎn)換為List ,而Integer[]就可以轉(zhuǎn)換為List 了吧。因為List中的泛型必須是引用類型,int是基本數(shù)據(jù)類型,不是引用類型,但int的包裝類型Integer是class類型,屬于引用類型,所以Integer可以作為List形參,List 在java中是可以存在的,但不存在List 類型。
在編碼時,我們不光要知其然,還要知其所以然,通過分析JDK源碼,才能得出一手信息,不僅了解到了如何用,還能得出為何這樣用。

我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流