Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

JavaÖÐtoArrayµÄÓ÷¨Ì½¾¿(javaÊý×éÓëlistת»»)


Ò»£®             Incident
import java.util.ArrayList;  
import java.util.List;  
public class Test {  
    public static void main(String[] args) {  
          
        List<String> list = new ArrayList<String>();   
        list.add("1");   
        list.add("2");   
        String[] tt =(String[]) list.toArray(new String[0]);   
    }  
}
 
Õâ¶Î´úÂëÊÇûÎÊÌâµÄ£¬µ«ÎÒÃÇ¿´µ½String[] tt =(String[]) list.toArray(new String[0]) ÖеIJÎÊýºÜÆæ¹Ö£¬È»¶øÈ¥µôÕâ¸ö²ÎÊýnew String[0]È´ÔÚÔËÐÐʱ±¨´í¡£¡£¡£
¶þ£®             Root Cause Analysis
¾­Ñо¿·¢ÏÖtoArrayÓÐÁ½¸ö·½·¨£º
public Object[] toArray() {  
   Object[] result = new Object[size];     
   System.arraycopy(elementData, 0, result, 0, size);     
   return result;  
}  
²»´ø²ÎÊýµÄtoArray·½·¨£¬Êǹ¹ÔìµÄÒ»¸öObjectÊý×飬Ȼºó½øÐÐÊý¾Ý¿½±´£¬´Ëʱ½øÐÐתÐ;ͻá²úÉúClassCastException£¬ÕâÒ²¾ÍÊÇÉÏÊöÎÊÌâµÄroot causeÁË¡£
 
 
public Object[] toArray(Object a[]) {     
if (a.length < size)         
a = (Object[])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;  
}
¶ø


Ïà¹ØÎĵµ£º

java½â»ó²¿·ÖºÃÌâ~~

·Ï»°²»¶à˵£¬Ö±½ÓÉÏÌâÄ¿
½ÓÏÂÀ´¾ÍÊǼûÖ¤Ææ¼£µÄʱ¿Ì£ºÇëÏȲ»ÒªÔËÐдúÂ룬²ÂÒ»ÏÂÊä³öµÄ½á¹û£º
1.
public class Test1{
    public static void main(String[] args){
        System.out.println(2.0 - 1.1);
    }
}
2.
public class Test2{
&n ......

java ö¾ÙÀà

  ö¾ÙÀàÐÍÊÇJDK5.0µÄÐÂÌØÕ÷¡£SunÒý½øÁËÒ»¸öȫеĹؼü×ÖenumÀ´¶¨ÒåÒ»¸öö¾ÙÀà¡£ÏÂÃæ¾ÍÊÇÒ»¸öµäÐÍö¾ÙÀàÐ͵Ķ¨Ò壺
¡¡¡¡Java´úÂë
¡¡¡¡public enum Color{
¡¡¡¡RED,BLUE,BLACK,YELLOW,GREEN
¡¡¡¡}
¡¡¡¡public enum Color{
¡¡¡¡RED,BLUE,BLACK,YELLOW,GREEN
     }
    ö¾ ......

java °²È«É³ÏäÄ£ÐÍÏê½â£¨×ª£©


Æðµ½µÚÒ»µÀ°²È«±£ÕÏ×÷ÓõĔ˫Ç×ίÅÉÀà¼ÓÔØÄ£ÐÍ”
Ë«Ç×ίÅÉ·½Ê½µÄÀà¼ÓÔØ£¬Ö¸µÄÊÇÓÅÏÈ´Ó¶¥²ãÆô¶¯Àà¼ÓÔØÆ÷¿ªÊ¼£¬×Ô¶¥Ïòϵķ½Ê½¼ÓÔØÀàµÄÄ£ÐÍ£¨²Î¼ûµÚÒ»ÌõÀà×°ÔØÆ÷Ìåϵ½á¹¹£©¡£
ÕâÖÖÄ£Ð͵ĺô¦ÊÇ£¬µ×²ãµÄÀà×°ÔØÆ÷×°ÔصÄÀàÎÞ·¨Ó붥²ãÀà×°ÔØÆ÷×°ÔصÄÀàÏ໥µ÷Óá£
ÄÄÅÂÊÇͬ°üϵÄÀֻ࣬ҪËûÃDz»ÊôÓÚͬһÀà×°ÔØÆ÷£¬ ......

ÈçºÎÓÃJAVA ¶ÔÏó£¿

ÈçºÎÓÃJAVA ¶ÔÏó£¿
д¼òµ¥Ï¸øÎÒ¿´Ï ллÁË
´´½¨Àà Ãû×Ö =new ÉèString ¼¸¸ö¶¼¿ÉÒÔ¡£ ÔÚÓø´ÖÆ Êä³ö
ÔÚÏÂÃæ¼ÌÐø´´½¨Àà ºóÃæ×îºÃ±ê×¢ÉÏ//ÕâÑù¿´ÆðÀ´¼òµ¥ÏÂллÁË
Çó½â¾ö ÀàºÍ¶ÔÏóûѧÃ÷°×ÀÏʦ½²µÄÎÒºýÍ¿ ÃÔºý×ö²»³öÀ´¡£ ......

Ò»¸öJAVA³ÌÐòÔ±µÄ¿´·¨

    ÍøÉϵÄÕâƪÎÄÕ£¬Ð´µÃºÜʵÔÚ£¬¹ÊÌùÁ˳öÀ´......
    ±¾ÈË×öÈí¼þ¿ª·¢´ó¸ÅÓУ¶Äê¶àÁË£¬´ÓÊÂjava¿ª·¢´ó¸Å£´Äê¶à£¬Ò»Ö±ÔÚÉϺ££®ÏÖÔÚÎÒÔÚÍøÉÏ×ÜÊÇ¿´µ½´ó¼ÒÔÚÌÖÂÛʲô¼Ü¹¹±Èʲô¼Ü¹¹ºÃ£¬Ê²Ã´¼¼Êõ±Èʲô¼¼ÊõÇ¿£® ¶ÔÕâ¸öÎÒÏë̸̸Îҵļ¸µã¿´·¨£®¡¡
   
    µÚÒ» ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ