今天寫代碼遇到一個奇怪的問題,具體代碼不貼出了,寫一個簡化的版本。以下:
java
ArrayList<String> list=new ArrayList<String>(); String strings[]=(String [])list.toArray();
這樣寫代碼我的以爲應該沒什麼問題,編譯也沒有問題。但是具體運行的時候報異常,以下:Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object;
可是這麼寫是沒有問題的:
數組
ArrayList<String> list=new ArrayList<String>(); String strings[]=new String[list.size()]; for(int i=0,j=list.size();i<j;i++){ strings[i]=list.get(i); }
對於這個現象咱們能夠這麼解釋:Java中容許向上和向下轉型,可是這個轉型是否成功是根據Java虛擬機中這個對象的類型來實現的。Java虛擬機中保存了每一個對象的類型。而數組也是一個對象。數組的類型是[Ljava.lang.Object。把[Ljava.lang.Object轉換成[Ljava.lang.String是顯然不可能的事情,由於這裏是一個向下轉型,而虛擬機只保存了這是一個Object的數組,不能保證數組中的元素是String的,因此這個轉型不能成功。數組裏面的元素只是元素的引用,不是存儲的具體元素,因此數組中元素的類型仍是保存在Java虛擬機中的。
安全
根據上面的解釋,咱們能夠把這個問題概括到下面這個模型:
app
Object objs[]=new Object[10]; String strs[]=(String[])objs;
這樣子和剛纔上面編譯錯誤是同樣的。若是咱們修改一下這個代碼,以下:
ui
String strs[]=new String[10]; Object objs[]=strs;
這樣子就能夠編譯經過了。因此這個問題咱們能夠歸結爲一個Java轉型規則的問題。下面談一下Java數組對範型的支持問題。spa
JDK5中已經有了對範型的支持,這樣能夠保證在集合和Map中的數據類型的安全,但是,List的toArray方法返回的居然是Object []讓人很迷惑。我的感受應該能夠根據範型,直接返回相應的T []。仔細看了一下JDK的源碼發現List轉化爲array有兩個方法:.net
public Object[] toArray();
這個方法把List中的所有元素返回一個相同大小的數組,數組中的全部元素都爲Object類型。
設計
public <T> T[] toArray(T[] a);
這個方法把List中的所有元素返回一個相同大小的數組,數組中的全部元素都爲T類型。
code
List如此設計是由於java編譯器不容許咱們new範型數組。也就是說你不能這麼定義一個數組:
orm
T arr=new T[size];
可是你卻能夠用T[]來表示數組,並且能夠把數組強制轉化爲T[]。好比List中的public <T> T[] toArray(T[] a)是這麼實現的:
public <T> T[] toArray(T[] a) { if (a.length < size) a = (T[])java.lang.reflect.Array. newInstance(a.getClass().getComponentType(), size); System.arraycopy(elementData, 0, a, 0, size); if (a.length > size) a[size] = null; return a; }
從上面代碼中能夠看到,由於你不知道這個數組的類型,你必須經過反射機制建立這個數組(a.getClass().getComponentType()方法是取得一個數組元素的類型)。
最終,List轉換爲Array能夠這樣處理:
ArrayList<String> list=new ArrayList<String>(); String[] strings = new String[list.size()]; //toArray(T[] a)這個方法返回一個類T的數組,這個數組包含了List內所有的元素。這個方法的特色就是,若是數組a的長///度可以裝下整個List的數據的時候,全部數據會放入到數組a當中。若是數組a的長度不夠長,那麼就會返回一個數組。 list.toArray(strings);
反過來,若是要將數組轉成List怎麼辦呢?以下:
String[] s = {"a","b","c"}; List list = java.util.Arrays.asList(s);
String[] ary = {"abc", "123", "45"}; StringBuffer sb = new StringBuffer(); for(int i = 0; i < ary.length; i++) { sb. append(ary[i]..trim()+","); } String newStr = sb.toString(); System.out.println(newStr.substring(0, str.length() - 1));
從網上找的一些方法:
數組轉成list:
方法 1.
String[] userid = {"aa","bb","cc"}; List<String> userList = new ArrayList<String>(); Collections.addAll(userList, userid);
方法 2.
String[] userid = {"aa","bb","cc"}; List<String> userList = Arrays.asList(userid);
注意:Arrays.asList()返回一個受指定數組支持的固定大小的列表。因此不能作Add、Remove等操做。
List list = new ArrayList(Arrays.asList(userid));這樣操做就能夠了。
方法3.
最笨的方法:
String[] userid = {"aa","bb","cc"}; List<String> userList = new ArrayList<String>(userid.length); for(String uid: userid){ userList.add(uid); }
list轉數組:
方法 1.
List<String> strList = new ArrayList<String>(); strList.add("aa"); strList.add("bb"); Object[] objs = strList.toArray(); -------------- //若是要變成String數組,須要強轉類型。可是下面這種寫法在運行的時候會報錯,編譯不會報錯 //String[] strs = (String[]) strList.toArray(); //應該這麼寫: List<Integer> test=new ArrayList<Integer>(); for(int i=0;i<5;i++){ test.add(i); } 方法1. 使用構造一個和集合的size()相等長度的數組t,而後調用toArray(T[] a)方法對數組t進行填充,返回類型爲T類型的數組 Integer[] t=new Integer[test.size()]; test.toArray(t); System.out.println(Arrays.asList(t));//打印數組調用的是object的toString()方法,這樣會看不出數組的內容 System.out.println(Arrays.asList(test.toArray(t))); 輸出: [0, 1, 2, 3, 4] [0, 1, 2, 3, 4] 方法2. toArray(T[] a)方法在調用的時候會對參數即數組a 會進行填充,同時還會返回一個數組,能夠利用這個特性來處理 在對參數數組t[]a進行填充的時候分幾種狀況: (1)若是數組a的長度和集合test的size()相等,那麼會對a進行徹底填充,即此時數組a的元素就是test中的元素,返回值就 (2)若是數組a的長度小於集合test的size(),那麼數組a不會被填充,即此時數組a中的長度爲0,那麼就爲[],爲1,就爲[null] (3)若是數組a的長度大於集合test的size(),那麼數組a將會被徹底填充,集合元素不夠填充的,會以null進行填充 Integer[] t2=new Integer[2];//數組長度小於test集合的長度,,參數數組a不會被填充 Integer[] t3=test.toArray(t2); System.out.println(Arrays.asList(t2)); System.out.println(Arrays.asList(t3)); 輸出: [null,null] [0, 1, 2, 3, 4] Integer[] t4 = new Integer[test.size()];//數組長度等於test集合的長度,參數數組a會被填充 Integer[] t5 = test.toArray(t4); System.out.println(Arrays.asList(t4)); System.out.println(Arrays.asList(t5)); 輸出: [0, 1, 2, 3, 4] [0, 1, 2, 3, 4] Integer[] t6 = new Integer[test.size()+2];//數組長度大於test集合的長度,參數數組a會被填充,不夠用null補充 Integer[] t7 = test.toArray(t6); System.out.println(Arrays.asList(t6)); System.out.println(Arrays.asList(t7)); 輸出: [0, 1, 2, 3, 4, null, null] [0, 1, 2, 3, 4, null, null] ----------------- 總結:使用toArray(T[] a)方法,參數數組a的長度小於集合test長度將不會被填充,若是大於或者等於將會被徹底填充;該方法返回的數組至少會包含list中的全部元素,在參數數組長度大於集合test長度的狀況下,返回的數組長度和被填充後的參數數組的值是同樣的
方法 2.
笨方法:
List<String> strList = new ArrayList<String>(); strList.add("aa"); strList.add("bb"); String[] strs = new String[strList.size()];