如题所述
1.List转æ¢æ为æ°ç»ãï¼è¿éçListæ¯å®ä½æ¯ArrayList)
è°ç¨ArrayListçtoArrayæ¹æ³ã
toArray
public <T> T[] toArray(T[] a)è¿åä¸ä¸ªæç §æ£ç¡®ç顺åºå å«æ¤å表ä¸ææå ç´ çæ°ç»ï¼è¿åæ°ç»çè¿è¡æ¶ç±»åå°±æ¯æå®æ°ç»çè¿è¡æ¶ç±»åãå¦æå表è½æ¾å ¥æå®çæ°ç»ï¼åè¿åæ¾å ¥æ¤å表å ç´ çæ°ç»ãå¦åï¼å°æ ¹æ®æå®æ°ç»çè¿è¡æ¶ç±»ååæ¤å表ç大å°åé ä¸ä¸ªæ°çæ°ç»ã
å¦ææå®çæ°ç»è½å®¹çº³å表并æå©ä½ç©ºé´ï¼å³æ°ç»çå ç´ æ¯å表çå¤ï¼ï¼é£ä¹ä¼å°æ°ç»ä¸ç´§è·å¨éåæ«å°¾çå ç´ è®¾ç½®ä¸º nullãè¿å¯¹ç¡®å®å表çé¿åº¦å¾æç¨ï¼ä½åª å¨è°ç¨æ¹ç¥éå表ä¸ä¸å å«ä»»ä½ null å ç´ æ¶ææç¨ã
æå®è ï¼
æ¥å£ Collection<E> ä¸ç toArray
æå®è ï¼
æ¥å£ List<E> ä¸ç toArray
è¦çï¼
ç±» AbstractCollection<E> ä¸ç toArray
åæ°ï¼
a - è¦åå¨å表å ç´ çæ°ç»ï¼å¦æå®è¶³å¤å¤§çè¯ï¼å¦åï¼å®æ¯ä¸ä¸ªä¸ºåå¨å表å ç´ èåé çãå ·æç¸åè¿è¡æ¶ç±»åçæ°æ°ç»ã
è¿åï¼
å å«å表å ç´ çæ°ç»ã
æåºï¼
ArrayStoreException - å¦æ a çè¿è¡æ¶ç±»åä¸æ¯æ¤å表ä¸æ¯ä¸ªå ç´ çè¿è¡æ¶ç±»åçè¶ ç±»åã
å ·ä½ç¨æ³ï¼
List list = new ArrayList();
list.add("1");
list.add("2");
final int size = list.size();
String[] arr = (String[])list.toArray(new String[size]);
è¿å¯ä»¥ä½¿ç¨è¿ä¸ªæ¹æ³
String[] userid = {"aa","bb","cc"};
List<String> userList = new ArrayList<String>();
Collections.addAll(userList, userid);
2.æ°ç»è½¬æ¢æ为Listã
è°ç¨ArraysçasListæ¹æ³.
asList
public static <T> List<T> asList(T... a)è¿åä¸ä¸ªåæå®æ°ç»æ¯æçåºå®å¤§å°çå表ãï¼å¯¹è¿åå表çæ´æ¹ä¼âç´åâå°æ°ç»ãï¼æ¤æ¹æ³å Collection.toArray ä¸èµ·ï¼å å½äºåºäºæ°ç»ç API ä¸åºäº collection ç API ä¹é´çæ¡¥æ¢ãè¿åçå表æ¯å¯åºååçï¼å¹¶ä¸å®ç°äº RandomAccessã
æ¤æ¹æ³è¿æä¾äºä¸ä¸ªå建åºå®é¿åº¦çå表ç便æ·æ¹æ³ï¼è¯¥å表被åå§å为å å«å¤ä¸ªå ç´ ï¼
List stooges = Arrays.asList("Larry", "Moe", "Curly");
åæ°ï¼
a - æ¯æå表çæ°ç»ã
è¿åï¼
æå®æ°ç»çå表è§å¾ã
å¦è¯·åè§ï¼
Collection.toArray()
å ·ä½ç¨æ³:
String[] arr = new String[] {"1", "2"};
List list = Arrays.asList(arr);
è°ç¨ArrayListçtoArrayæ¹æ³ã
toArray
public <T> T[] toArray(T[] a)è¿åä¸ä¸ªæç §æ£ç¡®ç顺åºå å«æ¤å表ä¸ææå ç´ çæ°ç»ï¼è¿åæ°ç»çè¿è¡æ¶ç±»åå°±æ¯æå®æ°ç»çè¿è¡æ¶ç±»åãå¦æå表è½æ¾å ¥æå®çæ°ç»ï¼åè¿åæ¾å ¥æ¤å表å ç´ çæ°ç»ãå¦åï¼å°æ ¹æ®æå®æ°ç»çè¿è¡æ¶ç±»ååæ¤å表ç大å°åé ä¸ä¸ªæ°çæ°ç»ã
å¦ææå®çæ°ç»è½å®¹çº³å表并æå©ä½ç©ºé´ï¼å³æ°ç»çå ç´ æ¯å表çå¤ï¼ï¼é£ä¹ä¼å°æ°ç»ä¸ç´§è·å¨éåæ«å°¾çå ç´ è®¾ç½®ä¸º nullãè¿å¯¹ç¡®å®å表çé¿åº¦å¾æç¨ï¼ä½åª å¨è°ç¨æ¹ç¥éå表ä¸ä¸å å«ä»»ä½ null å ç´ æ¶ææç¨ã
æå®è ï¼
æ¥å£ Collection<E> ä¸ç toArray
æå®è ï¼
æ¥å£ List<E> ä¸ç toArray
è¦çï¼
ç±» AbstractCollection<E> ä¸ç toArray
åæ°ï¼
a - è¦åå¨å表å ç´ çæ°ç»ï¼å¦æå®è¶³å¤å¤§çè¯ï¼å¦åï¼å®æ¯ä¸ä¸ªä¸ºåå¨å表å ç´ èåé çãå ·æç¸åè¿è¡æ¶ç±»åçæ°æ°ç»ã
è¿åï¼
å å«å表å ç´ çæ°ç»ã
æåºï¼
ArrayStoreException - å¦æ a çè¿è¡æ¶ç±»åä¸æ¯æ¤å表ä¸æ¯ä¸ªå ç´ çè¿è¡æ¶ç±»åçè¶ ç±»åã
å ·ä½ç¨æ³ï¼
List list = new ArrayList();
list.add("1");
list.add("2");
final int size = list.size();
String[] arr = (String[])list.toArray(new String[size]);
è¿å¯ä»¥ä½¿ç¨è¿ä¸ªæ¹æ³
String[] userid = {"aa","bb","cc"};
List<String> userList = new ArrayList<String>();
Collections.addAll(userList, userid);
2.æ°ç»è½¬æ¢æ为Listã
è°ç¨ArraysçasListæ¹æ³.
asList
public static <T> List<T> asList(T... a)è¿åä¸ä¸ªåæå®æ°ç»æ¯æçåºå®å¤§å°çå表ãï¼å¯¹è¿åå表çæ´æ¹ä¼âç´åâå°æ°ç»ãï¼æ¤æ¹æ³å Collection.toArray ä¸èµ·ï¼å å½äºåºäºæ°ç»ç API ä¸åºäº collection ç API ä¹é´çæ¡¥æ¢ãè¿åçå表æ¯å¯åºååçï¼å¹¶ä¸å®ç°äº RandomAccessã
æ¤æ¹æ³è¿æä¾äºä¸ä¸ªå建åºå®é¿åº¦çå表ç便æ·æ¹æ³ï¼è¯¥å表被åå§å为å å«å¤ä¸ªå ç´ ï¼
List stooges = Arrays.asList("Larry", "Moe", "Curly");
åæ°ï¼
a - æ¯æå表çæ°ç»ã
è¿åï¼
æå®æ°ç»çå表è§å¾ã
å¦è¯·åè§ï¼
Collection.toArray()
å ·ä½ç¨æ³:
String[] arr = new String[] {"1", "2"};
List list = Arrays.asList(arr);
温馨提示:答案为网友推荐,仅供参考